I have the following predefined code:
class WeekDayError(Exception):
pass
class Weeker:
#
# Write code here
#
def __init__(self, day):
#
# Write code here
#
try:
weekday = Weeker('Monday')
print(weekday)
except WeekDayError:
print("Sorry, I can't serve your request.")
My task is now to fill in the code so that if Monday is entered the exception is raised and the print statement is printed to the screen.
I tried the following:
class WeekDayError(Exception):
pass
class Weeker:
list=["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
def __init__(self, day):
if day not in Weeker.list:
WeekDayError()
try:
weekday = Weeker('Monday')
print(weekday)
except WeekDayError:
print("Sorry, I can't serve your request.")
However, when I run my code the exception is not raised. Where is my mistake?
I can see that the object information is printed. The statement containing the exception class WeekDayError
with the pass is given, so this is not to be modified. So the mistake must be in what I have written here:
if day not in Weeker.list:
WeekDayError()
But as far as I can see the condition is true (Monday is not in the list) so the WeekDayError() class is called.
CodePudding user response:
raise
it:
def __init__(self, day):
if day not in Weeker.list:
raise WeekDayError()