-1

When I run this code, even if enter '1' or '2' or '3', my code still runs through the while loop. How do I change my code so that it will only run through the while loop if the user DOESN'T enter '1', '2' or '3'? Thank you very much!

column=input("What column is your card in? Please enter either '1', '2' or '3':")
while column != "1" or "2" or "3": 
    print("That is an invalid choice. You must enter either '1' or '2' or '3'")
    column=input("What column is your card in? Please enter either '1', '2' or '3':")
print(column)
  • 1
    Use `column not in {'1', '2', '3'}`; computers are not human and English grammar is hard for computers. – Martijn Pieters Nov 10 '18 at 22:26
  • The issue is that the condition IS actually true, because it is not what you expect. the "condition != 1" part evaluates, and then gets combined with or "2". Now, booleans accept and work with strings, non empty strings are considered Truthy. Combine this with the following functionality of "or" internally: For ‘or’ operator if left value is true, then it is returned, otherwise if left value is false, then right value is returned. And there you go. Hope that helps. Because of "or" 2, the overall condition will always evaluate to True. – Paritosh Singh Nov 10 '18 at 22:32
  • Thank you so much! My code now works @MartijnPieters – Atara Klein Nov 11 '18 at 14:36

1 Answers1

0

Your condition should be for each check

while (column != "1") or (column != "2") or (column != "3"):

Without this check you say that it is non zero and hence True, the condition is satisfied for the while loop. Hence the conditions are met. 2 or 3 is not compared with column.

Ashok KS
  • 659
  • 5
  • 21