0

I am having trouble with my syntax around if statements and conditionals. I am trying to write a function which returns what time to set an alarm clock to depending on two parameters, what day it is and whether the person is on vacation or not. My days are encoded as 0 = Sunday, 1 = Monday ... 6 = Saturday. The function needs to return '7:00' for weekdays while not on vacation, '10:00' during weekdays on vacation and weekends while not on vacation, and finally return 'off' on weekends not on vacation. So far I have the following code but it gives me '10:00' when I call the function with print(alarm_clock(0, True)) when it is supposed to output 'off' instead. Any help appreciated. Thanks

def alarm_clock(day, on_vacation):
    """Alarm clock function"""

    if (int(day) < 6 and int(day) != 0) and not on_vacation:
        return('7:00')

    elif (int(day) == 6 or int(day) == 0) and not on_vacation:
        return('10:00')

    elif (int(day) < 6 or int(day) != 0) and on_vacation:
        return('10:00')

    elif (int(day) == 6 or int(day) == 0) and on_vacation:
        return('off')
1
  • 2
    in the 3rd elif statement, change the 'or' to 'and' Commented Sep 13, 2017 at 14:25

3 Answers 3

1

int(day) < 6 or int(day) != 0 will always be true for any value of day, because every value (including 0) is either less than 6, or if it is not less than 6 it is also not equal to 0.

A clearer way to write this sort of thing is to use a chained comparison for the ranges or tuple membership for the disjoint ones:

def alarm_clock(day, on_vacation):
    """Alarm clock function"""
    day = int(day)
    if 0 != day < 6 and not on_vacation:
        return '7:00'

    elif day in (6, 0) and not on_vacation:
        return '10:00'

    elif 0 != day < 6 and on_vacation:
        return '10:00'

    elif day in (6, 0) and on_vacation:
        return 'off'

Also extracting out the common int(day) and removing the spurious parentheses makes the code a bit cleaner.

Sign up to request clarification or add additional context in comments.

Comments

0

Change or to and in 3rd condition:

def alarm_clock(day, on_vacation):
    """Alarm clock function"""

    if (int(day) < 6 and int(day) != 0) and not on_vacation:
        return('7:00')

    elif (int(day) == 6 or int(day) == 0) and not on_vacation:
        return('10:00')

    elif (int(day) < 6 and int(day) != 0) and on_vacation:
        return('10:00')

    elif (int(day) == 6 or int(day) == 0) and on_vacation:
        return('off')

Comments

0

In the third statement, the or should be an and. print(alarm_clock(0, True)) was getting caught at the third statement because the day was less than six even though it was also day 0.

def alarm_clock(day, on_vacation):
   """Alarm clock function"""

   if (int(day) < 6 and int(day) != 0) and not on_vacation:
       return('7:00')

   elif (int(day) == 6 or int(day) == 0) and not on_vacation:
       return('10:00')

   elif (int(day) < 6 and int(day) != 0) and on_vacation:
       return('10:00')

   elif (int(day) == 6 or int(day) == 0) and on_vacation:
       return('off')

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.