Reputation: 7
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.
Upvotes: 1
Views: 69
Reputation:
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....if
if blocks, use if....elif....elif....else
statement
Upvotes: 0
Reputation: 140613
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.
Upvotes: 3
Reputation: 94
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"
Upvotes: -1
Reputation: 647
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
Upvotes: 0
Reputation:
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"
Upvotes: 0
Reputation: 71610
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
Upvotes: 0