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.

  • You might want to read about operator precedence. https://docs.python.org/3/reference/expressions.html#operator-precedence The operator "and" is getting executing first. You can put parantheses around the day == 0 or day == 6 condition. – Prakhar Varshney Aug 20 '21 at 09: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`. – Matthias Fripp Aug 20 '21 at 09:26

6 Answers6

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.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • 1
    That was exactly what I was going to post! Anyway +1 –  Aug 20 '21 at 09:25
  • 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? – user16712533 Aug 20 '21 at 09:45
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
U13-Forward
  • 69,221
  • 14
  • 89
  • 114
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"
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

Mig B
  • 637
  • 1
  • 11
  • 19
  • 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? – user16712533 Aug 20 '21 at 09:32
  • AND is proceeded before OR, check [this post](https://stackoverflow.com/questions/16679272/priority-of-the-logical-statements-not-and-or-in-python) for further explanation. – Mig B Aug 20 '21 at 09:58
  • 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" ? – user16712533 Aug 20 '21 at 10:16
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

-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"
Amir Gh
  • 84
  • 1
  • 4