This question already has answers here:
How to test multiple variables against a value?
(24个答案)
三年前关闭。
我正在为我的计算机编程课完成一个关于pygame的作业;我对这类事情不是特别精通,所以如果我在提问时犯了一些错误,请原谅。
我试图修改我们教科书中的一些源代码,目的是允许用户设置一组块在窗口中移动的速度,以及位置更新之间的延迟。在程序的开头是一个while循环,设计用来让用户在预定设置之间进行选择,或者创建自己的设置。
choice = ""
while choice != "E" or "e" or "Elect" or "elect" or "ELECT" or "C" or "c" or "Create" or "create" or "CREATE" :
    choice = input("Would you like to elect a speed setting for the vectors, or create your own? (Type 'E' for elect or 'C' for create) ")

当我尝试在shell中运行程序时,在输入'e'时,它再次给了我while循环的输入语句。为什么“choice”没有设置为我的输入?

最佳答案

尝试:

choice = ""
while choice.lower() not in ("e", "elect", "c", "create"):
    choice = input("Would you like to elect a speed setting for the vectors,, create your own? (Type 'E' for elect, 'C' for create) ")

为什么?
你的问题是choice != "E" or "e" or ...意味着(choice != "E") or ("e") or (...)。由于它不是"e"TrueNone或空序列,因此其计算结果为0。所以你的条件总是正确的。

关于python - 为什么我的while循环没有终止? ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36667006/

10-12 00:26
查看更多