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:
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.")
CodePudding user response:
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 % 7
is 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
EDIT: Some detail on the design choices:
I put the days of the week outside of the constructor because it does not depend on input data, however it is very specific to the class so it was more sensible to implement it as a class variable rather than a global variable. However, leaving it inside the constructor or storing it as global variable would have done the job too.
I used the try
block primarily because you used one, and I tried to stay close to your code.
It works out because the .index(element)
method of python lists gives you the index of element
if it is present in the list, if it is not, it will raise the error ValueError
which we catch (since we expect it) and that we replace with the custom error you designed.