Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Using both OR, AND in an IF-statement – Python

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))

Why does this return "10.00"? In my mind it should return "off". Yes, day is equal to 0, but vacation is True, and the IF-statements first line states that it should only be executed if vacation is not True.

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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:
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading