-1

I am having trouble figuring out a piece of code that looks like it should be working but it isn't working how I'd like it too.

I have made an example loop to demonstrate the issue I am having:

a = input("Answer: ")

while a != '1' or '2':
    print("That's incorrect try again")
    a = input("Answer: ")

The intent would be that I have the user input the condition till it was right but when I put in the correct answer this is what happens.

Answer: 3
That's incorrect try again
Answer: 1
That's incorrect try again
Answer: 2
That's incorrect try again

This is the python shell output and as you can see when I put the wrong number in I get the right message as it is wrong but when I put the 2 correct answers in neither make it jump out of the loop.

Thanks in advance

Kootch
  • 31
  • 5

1 Answers1

1

This is always True

a != '1' or '2':

because it works like:

(a != '1') or '2' 

and '2' is True

Darth Kotik
  • 2,261
  • 1
  • 20
  • 29