Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Need to create a class which will be able to store and to manipulate days of a week

I came through an exercise in a python course where task is to implement a class called Weeker.
this class will be able to store and to manipulate days of a week.
The class constructor accepts one argument – a string.

The string represents the name of the day of the week and the only acceptable values must come from the following set: Mon Tue Wed Thu Fri Sat Sun
Invoking the constructor with an argument from outside this set should raise the WeekDayError exception

The class should provide the following facilities:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

1.objects of the class should be "printable", i.e. they should be able to implicitly convert themselves
2.into strings of the same form as the constructor arguments;
3.the class should be equipped with one-parameter methods called add_days(n) and subtract_days(n),
with n being an integer number and updating the day of week stored inside the object in the way
reflecting the change of date by the indicated number of days, forward or backward.
all object’s properties should be private.

I am not able to figure out how to go about it as I am new to Python.
This is what I got so far:

class WeekDayError(Exception):
    pass
    
class Weeker:
    def __init__(self, day):
        self.__day= list(day)
        self.__day=['Mon','Tue','Wed','Thu','Fri','Sat','Sun']
        try:
            self.__current in self.__day
        except:
            raise WeekDayError
                    
    def __str__(self):
        return self.__current

    def add_days(self, n):
        self.__day = (self.__current + n) % 7

    def subtract_days(self, n):
        self.__day = (self.__current - n) % 7


try:
    weekday = Weeker('Mon')
    print(weekday)
    weekday.add_days(15)
    print(weekday)
    weekday.subtract_days(23)
    print(weekday)
    weekday = Weeker('Monday')
except WeekDayError:
    print("Sorry, I can't serve your request.")

>Solution :

There are a few problems with your code, for example:

self.__day= list(day)
self.__day=['Mon','Tue','Wed','Thu','Fri','Sat','Sun']

The second line erased what you tried to do with the first one: you overwrote the value you stored.

self.__day = (self.__current + n) % 7

The idea of using % 7is the right one, however you are trying to add a string with an integer, python doesn’t yet support that.

try:
   self.__current in self.__day
except:
   raise WeekDayError

The line inside the try clause won’t raise an error, as the operator in only returns True and False. Therefore your except clause will never apply.

Here is something that should work out:

class Weeker:
    WEEK_DAYS = ['Mon','Tue','Wed','Thu','Fri','Sat','Sun']
    def __init__(self, day):
        try:
            self.__current = self.WEEK_DAYS.index(day)
        except ValueError:
            raise WeekDayError
                    
    def __str__(self):
        return self.WEEK_DAYS[self.__current]

    def add_days(self, n):
        self.__current = (self.__current + n) % 7

    def subtract_days(self, n):
        self.__current = (self.__current - n) % 7
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading