初学者输入验证问题

时间:2019-09-03 12:40:53

标签: python

编写一个程序,提示用户输入单词序列。然后,程序将显示一个唯一词列表(仅出现一次,即不再重复的词)。

我尝试摆脱输入验证,但是如果用户输入了任何值,那么就尝试摆脱输入验证。程序中断。

list5 = []
game_over = False

while game_over is False:
    user_input = input("Please enter a word: ")
    list5.append(user_input)
    keep_it_going = input("Would you like to enter more words? (Y / N): ")
    while len(keep_it_going) != 1 and keep_it_going.lower() != "y" or "n":
        print("You entered an invalid value, please try again!")
        keep_it_going = input("Would you like to enter more words? (Y / N): ")
    if keep_it_going.lower() == "y":
        continue
    elif keep_it_going == "n":
        game_over = True

我希望程序能够运行,因为我看不到任何逻辑失误,但是,一旦我输入“ Y”代表“您想输入更多单词?(Y / N):”,该程序就会告诉您我输入的值无效。

1 个答案:

答案 0 :(得分:0)

while len(keep_it_going) != 1 and keep_it_going.lower() != "y" or "n":

是您的问题。输入的错误输入既不能是“ y”也不能是“ n”,因此您需要将其更改为以下内容:

while len(keep_it_going) != 1 or keep_it_going.lower() != "y" and keep_it_going.lower() != "n":
相关问题