0

When I run this program like this:

jonathan = 15 
anthony = 25 

if (jonathan or anthony) >= 21:
    print("They can enter the building together.")
else:
    print("They are not allowed to enter the building together.")

It outputs: They are not allowed to enter the building together.

However, when I run the program like this:

jonathan = 15 
anthony = 25 

if (anthony or jonathan) >= 21:
    print("They can enter the building together.")
else:
    print("They are not allowed to enter the building together.")

It outputs: They can enter the building together.

I was under the impression that the placement of the variable in this conditional if statement didn't matter if I was using the or keyword, since a conditional test with the or keyword passes if either one of the variables pass the test.

I do notice however that when I run the program like this, where jonathan is first again, and everything is included in the parentheses:

jonathan = 15 
anthony = 25 

if (jonathan or anthony >=21):
    print("They can enter the building together.")
else:
    print("They are not allowed to enter the building together.")

It outputs: They can enter the building together.

It would be greatly appreciated if one or more of you could explain to me the reasoning behind why the positioning of the variable & parentheses matters here in this condition with or.

I'm by no means an expert but from my point of view, it would probably be safest to use the last method where everything is in parentheses, including the >=21.

Thank you.

  • I know that when you get rid of the parentheses entirely and even if jonathan is positioned first in the conditional statement, it outputs 'They can enter the building together.` But I am kind of dumbfounded because when I did a similar or conditional with no parentheses, comparing age, it outputted the incorrect statement. I might post an example about this for further clarification. – AspriringProgrammer Feb 20 '22 at 02:45
  • You want `jonathan >= 21 or anthony >= 21`. – BrokenBenchmark Feb 20 '22 at 02:46
  • @BrokenBenchmark ^^^ That makes more sense, it's even in the book I'm using to learn lol. I just thought I could make the conditional statement simpler by getting rid of the second instance of >=21 like above, but I guess it makes more sense to do it as you commented. – AspriringProgrammer Feb 20 '22 at 02:51
  • 1
    To be clear, `jonathan >= 21 or anthony >= 21` and `(jonathan or antony) >= 21` are two expressions that are semantically different. – BrokenBenchmark Feb 20 '22 at 02:53
  • `(jonathan or antony) >= 21` sees whether either integer is [truthy](https://stackoverflow.com/questions/39983695/17769815). Depending on that, the result is interpreted as either [0 or 1](https://stackoverflow.com/q/2764017/17769815), and then compared against `21`. This is almost certainly _not_ what you're looking for. – BrokenBenchmark Feb 20 '22 at 02:56
  • ^^^@BrokenBenchmark Thanks for this response, very informative and helpful. – AspriringProgrammer Feb 20 '22 at 03:04
  • No problem! Happy to help. Have a great evening! :) – BrokenBenchmark Feb 20 '22 at 03:13

1 Answers1

0

Python evaluates boolean conditions lazily meaning python only evaluates what is necessary to determine the boolean value of the statement

For example

True or 0/0
>>> True

since True or ... evaluates to True, Python stops evaluating the statement after it sees the first True

Similarly

False and 0/0
>>> False

since False and ... evaluates to False, Python stops evaluating the statement after it sees the first False

Using this to describe the behavior in your example

(jonathan or anthony) >= 21
(15 or 25) >= 21
15 >= 21
False

and

(anthony or jonathan) >= 21
(25 or 15) >= 21
25 >= 21
True

Since positive integers evaluate to True, the or statements return the first value and compare it 21.

In the last example

(jonathan or anthony >= 21)
(15 or 25 >= 21)
15
True

you just get the value of jonathan, a positive integer, so the statement evaluates to True

That being said, I assume this is not the behavior you want. I assume you want to compare the values of both anthony and jonathan to 21, and the syntax for that is:

anthony >= 21 or jonathan >= 21
somebody3697
  • 206
  • 2
  • 4