Given a day of the week encoded as 0=Sun, 1=Mon, 2=Tue, ...6=Sat, and a boolean indicating if we are on vacation, return a string of the form "7:00" indicating when the alarm clock should ring.
Weekdays, the alarm should be "7:00" and on the weekend it should be "10:00". Unless we are on vacation -- then on weekdays it should be "10:00" and weekends it should be "off"
def alarm_clock(day, vacation):
if not vacation and 1<=day<=5:
return "7:00"
if not vacation and day==0 or day==6:
return "10:00"
if vacation and 1<=day<=5:
return "10:00"
if vacation and day==0 or day==6:
return "off"
Why does alarm_clock(6, False) → '10:00' but alarm_clock(6, True) → '10:00' instead of 'off'?
I know the correct answer but I'm still confused to why my initial logic is off.
6 Answers 6
The condition that is failing you:
if not vacation and day==0 or day==6:
You think the above means:
if not vacation and (day==0 or day==6):
but it actually does this:
if (not vacation and day==0) or day==6:
That is because the operator and has higher precedence than or, see here for example.
2 Comments
You can't use or like that, try with in:
def alarm_clock(day, vacation):
if not vacation and 1<=day<=5:
return "7:00"
if not vacation and day in [0, 6]:
return "10:00"
if vacation and 1<=day<=5:
return "10:00"
if vacation and day in [0, 6]:
return "off"
Example:
print(alarm_clock(6, True))
Output:
off
Comments
you need to add parenthesis as follows:
def alarm_clock(day, vacation):
if not vacation and 1<=day<=5:
return "7:00"
if not vacation and (day==0 or day==6):
return "10:00"
if vacation and 1<=day<=5:
return "10:00"
if vacation and (day==0 or day==6):
return "off"
Comments
You have to concat the or statement within your logic operator test like
def alarm_clock(day, vacation):
if not vacation and 1<=day<=5:
return "7:00"
if not vacation and (day==0 or day==6):
return "10:00"
if vacation and 1<=day<=5:
return "10:00"
if vacation and (day==0 or day==6):
return "off"
otherwise not vacation and day==0 or day==6 would get True for both cases, as the or day==6 part is always True.
print(alarm_clock(6,False))
10:00
print(alarm_clock(6,True))
off
3 Comments
This statement is actually evaluating as:
if (not vacation and day==0) or day==6:
Because python evaluates and before or.
By putting vacation as True and day=6, both the arguments evaluate to False because not True is False and day is not equal to 6. So and evaluated to False
Next, it moves to or day==6. And it notices that it is in-fact valid. So it goes to execute the statements under it.
So it returns '10:00'.
A tip:
Instead of using different if....if....ifif blocks, use if....elif....elif....else statement
Comments
add parenthesis between your ORs and AND you must change your code as follow:
def alarm_clock(day, vacation):
if not vacation and 1<=day<=5:
return "7:00"
if not vacation and (day==0 or day==6):
return "10:00"
if vacation and 1<=day<=5:
return "10:00"
if vacation and (day==0 or day==6):
return "off"
andconditions beforeor. Put parentheses around yourorconditions to force them to be evaluated before theand. In other words, in Python,False and False or Trueis evaluated as(False and False) or True, which isTrue, butFalse and (False or True)would beFalse.