控制用户输入

时间:2016-10-07 01:43:30

标签: python

我想知道为什么我的输入控件在这里不起作用。我只用userOption != "1"尝试了它并且工作正常(旁注:忽略其他功能。)

userOption = str(input("Chose option 1, 2 or \"E/e to exit\": "))
print(userOption)
while(userOption != "1" or userOption != "2" or userOption != "e"
        or userOption != "E"):
    reply = (userOption + " is not a valid option")
    print(reply)
    userOption = input("Chose option 1, 2 or \"E/e to exit\": ")
    if userOption == "1":
        validEntry("Celsius")
        C_to_F(int(userInput))
        print(F)
    elif userOption == "2":
        validEntry("Fahrenheit")
        F_to_C(int(userInput))
        print(C)
    else:
        input("Thank you. Press enter to close this screen in the exe mode.")

2 个答案:

答案 0 :(得分:1)

您必须使用and代替or

while userOption != "1" and userOption != "2" and userOption != "e" and userOption != "E":

not==

while not(userOption == "1" or userOption == "2" or userOption == "e" or userOption == "E"):

或者您可以使用not in

while userOption not in ("1", "2", "e", "E"):

答案 1 :(得分:0)

while(userOption != "1" or userOption != "2" or userOption != "e"
    or userOption != "E"):

无论用户输入什么,该逻辑都会产生真实的结果。正如其他人所说,你需要在这里使用and而不是or