datagoat
datagoat

Reputation: 63

Why does this while loop not break?

This loop does not break if h,l, or c is input:

x = input('enter h,l, or c')
while (x != 'c') or (x != 'h') or (x != 'l'):
    print('Sorry I didn't understand. Please enter h,l, or c')
    x = input("enter h,l, or c")

What I was intending can be solved this way:

x = input("enter h,l, or c")
while True:
    if x == 'c' or x == 'h' or x == 'l':
        break
    else:
        print('Sorry I didnt understand. Please enter h,l, or c')
        x = input("enter h,l, or c")

What is incorrect of the first piece of code? Does X not get evaluated at the beginning of the while?

Upvotes: 0

Views: 101

Answers (5)

dolgom
dolgom

Reputation: 631

Because of an error on the logic operation.

not (A or B)

This logic equals to

(not A) and (not B)

So the first code should be

x = input('enter h,l, or c')
while (x != 'c') and (x != 'h') and (x != 'l'):
    print("Sorry I didn't understand. Please enter h,l, or c")
    x = input("enter h,l, or c")

Upvotes: 2

ayivima
ayivima

Reputation: 98

Your while loop will always evaluate to True

0 x = input('enter h,l, or c')
1 while (x != 'c') or (x != 'h') or (x != 'l'):
2    print('Sorry I didn't understand. Please enter h,l, or c')
3    x = input("enter h,l, or c")

Your code has become something like this:

0 x = input('enter h,l, or c')
1 while True:
2    print('Sorry I didn't understand. Please enter h,l, or c')
3    x = input("enter h,l, or c")

Let's explain.

INPUT SCENARIOS:

a. If input is 'z', z is not equal to any of the letters so it becomes True for all the conditions. This means any input that is not one of 'h', 'l', 'c', will evaluate to True.

b. If input is 'h', h is neither equal to l nor c. This evaluates to a True OR False OR True scenario and obviously becomes a True. Thus, if your input is also any of the letters specified, it will be a True because it is not equal to the other letters in the condition, and just a single True is required for an OR condition to evaluate to True.

So your current code will ALWAYS evaluate to True, and the loop will run infinitely. You need to use AND instead of OR, use the second code you posted, or you can use recursion.

RECOMMENDED OPTIONS:

  1. Use AND
x = input('enter h,l, or c')
while (x != 'c') and (x != 'h') and (x != 'l'):
    print("Sorry I didn't understand. Please enter h,l, or c")
    x = input("enter h,l, or c")

  1. Recursion
def checker():
    x = input("enter h,l, or c")
    if (x != 'c') and (x != 'h') and (x != 'l'):
        print("Sorry I didn't understand. Please enter h,l, or c")
        checker()

checker()

Upvotes: 1

Prune
Prune

Reputation: 77837

Look at your condition:

while (x != 'c') or (x != 'h') or (x != 'l'):

Consider the case where the input character is c. The first condition is False, but the other two are True. F or T or T is True.

You need and connectors in your condition. Better yet, try

while not x in ['h', 'l', 'c']:

Upvotes: 3

Siong Thye Goh
Siong Thye Goh

Reputation: 3586

You should have used and condition rather than or. That is if it is one of the accepted letters, then (x != 'c'), (x != 'h'), and (x != 'h') are evaluated to be false.

x = input('enter h,l, or c')
while (x != 'c') and (x != 'h') and (x != 'l'):
    print("Sorry I didn't understand. Please enter h,l, or c")
    x = input("enter h,l, or c")

Upvotes: 3

tantalum
tantalum

Reputation: 2452

Let start with the statement that false or true is evaluated to true. Therefore if x is c then the (x != 'c') will be false, but the second case (x != 'h') will be true, and according to our first statement the whole or expression will evaluate to true so your loop will never exit. Instead what you need to is:

x = input('enter h,l, or c')
while not ((x == 'c') or (x == 'h') or (x == 'l')):
    print("Sorry I didn't understand. Please enter h,l, or c")
    x = input("enter h,l, or c")

Upvotes: 1

Related Questions