Why does 'OR' not work here?


#1

In the lession - Loops - Simple Errors

The loop seems to run, regardless of the input used.

if input is y or n, i’d expect the code to exit in this case. it doesn’t.

my code is below.

python

choice = raw_input('Enjoying the course? (y/n)')

while choice != "y" or "n":  # Fill in the condition (before the colon)
  choice = raw_input("Sorry, I didn't catch that. Enter again: ")

#2

assumed knowledge: the while loop will run as long as the while condition evaluates to true

there are two problems, by doing or "n" python will simply evaluate if "n" is considered true, which it is:

if "n":
    print "strings are considered true"

you need to do comparison at both sides of the or operator

with or, if the user makes an invalid choice (for example x) you get:

while "x" != "y" or "x" != "n":

which results in:

while True or True: 

which is true, so the loop will keep running

if the user enters a valid choice (y for example):

while "y" != "y" or "y" != "n":

we get:

while False or True:

False or True evaluates to true:

print False or True

so the loop keeps running, so using or the loop will short circuit on true


#3

Thanks for the reply.

Changing the code to evaluate the variable separately, doesn’t work either.

choice = raw_input('Enjoying the course? (y/n)')

while choice != "y" or choice != "n":  # Fill in the condition (before the colon)
  choice = raw_input("Sorry, I didn't catch that. Enter again: ")

changing the code to remove the second condition worked fine, the code exits on y input and any other input triggers the loop.

refreshed the page, with the above code saved and ran it a few times. the suggested correct code was as the first snipp above but with AND replacing OR.

This code works as expected, and I really don’t see why. In any case, the choice variable cannot be both y and n at the same time, in any single input, so therefore this shouldn’t be the case.

can you clearly explain?


#4

I walked through the entire loop (with invalid and valid input) to demonstrate why the or operator isn’t working here? You can walk the same steps to see why the and operator is working

and doesn’t evaluate both at the same time, there is a difference in how True or False (evaluates to true) and True and False (evaluates to false) is handled

now you can walk through the steps with and what i did with or


#5

I think I understand.

Because i’m evaluating the variable a second time for the while condition, i’m not triggering a short-circuit in the logic for it to jump out of the code when the first condition is met (because I assume that all conditions in a ‘while’ must be true for the to trigger the loop… so while y != y is false, y != n is true, and this means that the whole whole condition becomes true.

TL:DR - All conditions in while condition are evaluated, and are triggered if one of them is true, if anything is false, it does not short circuit the code.

Thanks, and hope this helps others.


#6

yes, because False (y != y) or True (y !=n) evaluates to true where as False (y != y) and True (y !=n) evaluates to false


#7

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.