user16712533
user16712533

Reputation: 7

Why does this function return a different value?

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

Answers (6)

user15801675
user15801675

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....ifif blocks, use if....elif....elif....else statement

Upvotes: 0

GhostCat
GhostCat

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

Amir Gh
Amir Gh

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

Mig B
Mig B

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

user14884311
user14884311

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

U13-Forward
U13-Forward

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

Related Questions