简体   繁体   English

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

[英]In python how do i use or in a while function

So i am trying to make this program where it asks for a username and password then it saves it, then you can log in with it and when you do lock in, it'll give you two options, you can type HELP or you can type COMMAND and if you type something else it will respond with "That is not a valid command" but when i do the while input3 != "HELP" or "LOCK" statement, it will just say that is not a valid command, even if i do type HELP or LOCK so what am I doing wrong and how can I use the or command 因此,我正在尝试使该程序在需要用户名和密码的位置进行保存,然后可以使用它登录,然后在锁定时为您提供两个选项,您可以输入HELP或键入COMMAND,如果您键入其他内容,它将以“那不是有效的命令”响应,但是当我执行while input3!=“ HELP”或“ LOCK”语句时,它只会说这不是有效的命令,即使如果我确实输入了HELP或LOCK,那么我在做什么错了,我该如何使用or命令

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")

Your while loop condition logic is flawed. 您的while循环条件逻辑有缺陷。 The or statement doesn't work the way you think it does. or语句无法按您认为的方式工作。 Do this instead: 改为这样做:

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

Read this famous stackoverflow question that explains the logic flaw in detail 阅读这个著名的stackoverflow问题 ,详细解释逻辑缺陷

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM