Using both OR, AND in an IF-statement - Python
Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Luau
--
Chapters
00:00 Question
00:35 Accepted answer (Score 17)
00:59 Answer 2 (Score 0)
01:14 Thank you
--
Full question
https://stackoverflow.com/questions/7276...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python
#avk47
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Luau
--
Chapters
00:00 Question
00:35 Accepted answer (Score 17)
00:59 Answer 2 (Score 0)
01:14 Thank you
--
Full question
https://stackoverflow.com/questions/7276...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python
#avk47
ACCEPTED ANSWER
Score 20
In Python and binds tighter than or. So your statement is equivalent to this:
if day == 0 or (day == 6 and vacation != True):
To get the correct result you must parenthesize the precedence yourself:
if (day == 0 or day == 6) and vacation != True:
ANSWER 2
Score 0
What you probably want is this:
def alarm_clock(day, vacation):
if (day == 0 or day == 6) and vacation != True:
return "10.00"
else:
return "off"
print(alarm_clock(0, True))