当不满足条件时,为什么我的代码通过我的while循环运行?

时间:2018-11-10 22:18:39

标签: python-3.x loops while-loop

当我运行此代码时,即使输入“ 1”或“ 2”或“ 3”,我的代码仍会在while循环中运行。如何更改我的代码,以便仅在用户不输入“ 1”,“ 2”或“ 3”的情况下才通过while循环运行?非常感谢你!

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 个答案:

答案 0 :(得分:0)

您的条件应针对每张支票

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

如果不进行此检查,则您说它不是零,因此为True,则while循环满足条件。因此满足条件。 2或3不与列比较。