在python中我如何使用或在while函数中

时间:2014-06-28 04:57:29

标签: python

所以我试图让这个程序在它要求输入用户名和密码然后保存它,然后你可以用它登录,当你锁定时,它会给你两个选项,你可以键入HELP或者您可以键入COMMAND,如果您输入其他内容,它将以&#34响应;这不是一个有效的命令"但当我做的时候输入3!="帮助"或" LOCK"声明,它只会说这不是一个有效的命令,即使我输入HELP或LOCK所以我做错了什么以及如何使用或命令

print("Hello, please register below")
user = input("Username: ")
password = input("Password: ")
print("Thank you for registering, please log in below")
print()
print("Hello, please log in below")
while 1 == 1:
    def menu():
        print("HELP for more options")
        print("LOCK to exit and secure system")

    input1 = None
    input2 = None
    input3 = None


    while input1 != user:
        input1 = input("Username: ")
    while input2 != password:
        input2 = input("Password ")
    menu()
    input3 = input("Command: ")
    while input3 != "HELP" or "LOCK":
        print("Please input a valid command")
        input3 = input("Command: ")
    if input3 == "HELP":
        print("Type LOCK to secure system and exit program, or tpye HELP to display this message again")
    input3 = input("Command: ")
    if input3 == "LOCK":
        print("System is now locked, exiting program")

1 个答案:

答案 0 :(得分:3)

您的while循环条件逻辑存在缺陷。 or语句不像您认为的那样工作。这样做:

while input3 not in ("HELP", "LOCK"):

阅读详细解释逻辑漏洞的this famous stackoverflow question