循环播放直到玩家输入匹配+奇怪的bug(?)

时间:2018-11-26 10:17:21

标签: python python-3.x

这个问题原本是关于如何编写一个while循环来询问输入的,直到用户给出正确的单词(我无法弄清楚),但是在我试图找出它的过程中(从帖子中这可能是重复的),我发现有些东西令我感到困惑。

while True:

    user_input = input("Enter 'test': ")

    # the line ("test") works fine but if I add the all-caps
    # version it no longer accepts ANYTHING and just keeps
    # printing "Invalid input!"
    if user_input == ("test", "TEST"):
        print("Correct input!")
        break
    else:
        print ("Invalid input!")

输出类似于:

"Enter 'test': "

>>> test

"Invalid input!"

编辑:还请注意,我是一个完整的初学者!我昨天刚学完所有东西(尽管很多东西都没有卡住,这就是为什么我在这里!)。

2 个答案:

答案 0 :(得分:1)

while True:

user_input = input("Enter 'test': ")

# the line ("test") works fine but if I add the all-caps
# version it no longer accepts anything and just keeps
# printing "Invalid input!"
if user_input == ("test" or "TEST"):
    print("Correct input!")
    break
else:
    print ("Invalid input!")

在为用户提供选择选项

时,使用“或”代替使用或

答案 1 :(得分:0)

您正在将字符串与元组匹配。这就是为什么它不断打印无效输入的原因。您可以在if语句中尝试以下代码以区分大小写:

if user_input.lower() == "test":
相关问题