Python尽管我插入正确的值[duplicate]

问题描述 投票:-1回答:1

我有这个代码:

choose = ""
while choose != "x" or choose != "X" or choose != "o" or choose != "O":
    choose = input("X or O? -> ")

但是即使用户插入x,X,o或O,它也会继续。

我是编码方面的新手,有谁想让它起作用?

python python-3.x while-loop
1个回答
3
投票

尝试一下

while choose not in [ "x" , "X" , "o" ,"O"]:

4个条件中的任何一个变为真都会继续循环。您可以使用由“或”表示的“和”。

相反,尝试使用Python的'in','not in'关键字。


0
投票

逻辑组合不是您想要的。[在此处输入图片描述] [1]如果输入O作为选择,则条件choose != "x"为真,则while循环。

我想你想要的是:

    choose = ""
    while not(choose == "x" or choose == "X" or choose == "o" or choose == "O"):
        choose = input("X or O? -> ")

有效〜/:D

© www.soinside.com 2019 - 2024. All rights reserved.