麻烦的while循环和选择

时间:2017-01-29 00:09:20

标签: python loops while-loop

selection=input("Which item do you use first? \n" +
    "a) Rescue flare \n" +
    "b) Pistol with 3 bullets \n" +
    "c) Machete knife \n")
while not selection != "a" or selection != "b" or selection != "c":
    print("You have an invalid option.  Please try again.")
    selection = input("Which item do you use first? \n" +
        "a) Rescue flare \n" +
        "b) Pistol with 3 bullets \n" +
        "c) Machete knife \n") 
if selection.lower()=="a":
    print("You see a ship passing by and shoot off the rescue flare. However, it seems like the ship did not see the flare.")

您好,

所以我决定使用循环来进行初学者分配。但是,我注意到现在我已经包含了一个循环(在这种情况下没有),当我运行它时,即使我输入有效选项(a,b,c),它也会将其读作无效选项。这是我设置循环或缩进导致此问题的方式吗?请告诉我。感谢我能得到的任何帮助。

1 个答案:

答案 0 :(得分:4)

while not selection != "a"包含双重否定因此转换为while selection == "a"您应将此行简化为:

while selection not in {'a','b','c'}:

相关问题