0
outcome=input("higher or lower")

while (outcome!= 'h' or outcome!= 'l'): 
    outcome=input("enter h or l")

print("its working")

the while loop keeps going even when i write H or L

1 Answer 1

6

You need and rather than or.

Let's assume the lowercase/uppercase issue is not a problem and you enter h rather than H.

Your expression will then be:

'h' != 'h' or 'h' != 'l'
\________/    \________/
   false   or    true
   \________________/
         true

Since an object cannot be two things at once, one of those inequalities must be true. Hence the entire expression must be true.

Hence you should change it to something like:

while (outcome != 'h') and (outcome != 'l'):

or:

while outcome not in ('h', 'l'):

the latter being more succinct as the number of possibilities increases:

while outcome not in ('n', 's', 'e', 'w', 'u', 'd', 'l', 'r'):
Sign up to request clarification or add additional context in comments.

3 Comments

yes thank you. i should have double checked before posting. thanks again :)
Another option: while outcome not in ('h', 'l'):
I would recomment @StevenRumbalski's answer, that I think that would be more pythonic way to do it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.