1

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.

asked Aug 20, 2021 at 9:17
2
  • You might want to read about operator precedence. docs.python.org/3/reference/… The operator "and" is getting executing first. You can put parantheses around the day == 0 or day == 6 condition. Commented Aug 20, 2021 at 9:23
  • Python evaluates and conditions before or. Put parentheses around your or conditions to force them to be evaluated before the and. In other words, in Python, False and False or True is evaluated as (False and False) or True, which is True, but False and (False or True) would be False. Commented Aug 20, 2021 at 9:26

6 Answers 6

3

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.

answered Aug 20, 2021 at 9:24
Sign up to request clarification or add additional context in comments.

2 Comments

That was exactly what I was going to post! Anyway +1
Ok I understand that writing: if not vacation and day==0 or day==6: inadvertently assigns (alarm_clock(6,True)) as '10:00' but why does it also assign (alarm_clock(6,False)) as '10:00' aswell?
0

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
answered Aug 20, 2021 at 9:21

Comments

0

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"
answered Aug 20, 2021 at 9:23

Comments

0

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

answered Aug 20, 2021 at 9:23

3 Comments

Ok I understand that writing: if not vacation and day==0 or day==6: inadvertently assigns (alarm_clock(6,True)) as '10:00' but why does it also assign (alarm_clock(6,False)) as '10:00' aswell?
AND is proceeded before OR, check this post for further explanation.
Without parenthesis, if not vacation and day==0 or day==6: return"10:00" is processed as "False and False or True" but alarm_clock(6, False) → '10:00' . Wouldn't "if vacation and (day==0 or day==6): return 'off' " assign it as "off" ?
0

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

answered Aug 20, 2021 at 9:32

Comments

-1

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"
answered Aug 20, 2021 at 9:25

1 Comment

How does this add anything to the answers already made?

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.