简体   繁体   English

如何修复由我在 Python 中的函数引起的这个无限循环?

[英]How can I fix this infinite loop caused by my function in Python?

I've seemed to create a bank account log in system that works correctly.我似乎创建了一个可以正常工作的银行帐户登录系统。 The only problem is that once a login is successful, the program would get stuck in a loop.唯一的问题是,一旦登录成功,程序就会陷入循环。 For example: Say, I create my account and set my username as "Hello" and my password as "123".例如:假设我创建了我的帐户并将我的用户名设置为“Hello”,我的密码设置为“123”。

The program will except these log in details but when I later try to log in with them, the infinite loop would happen.该程序将排除这些登录详细信息,但是当我稍后尝试使用它们登录时,就会发生无限循环。

I've tried amending this problem by plugging in return/global values for status and even put them in as many places in my program as possible but the infinite loop problem still persists.我已经尝试通过插入状态的返回/全局值来修正这个问题,甚至将它们放在我程序中尽可能多的地方,但无限循环问题仍然存在。 Could you please help me find why the loop keeps executing?你能帮我找出循环不断执行的原因吗?


users = {}
status = ""

                            #--------------- Login menu -----------------#
def displayMenu():
    global status

    status = input("Are you a registered user? \n1 - Yes \n2 - No \nQ - Quit \n")  
    if status == '1':
        oldUser()
    elif status == '2':
        newUser()
    return status


def mainMenu():
    print("Hello account holder", login,"what service would  you like to use today?")

                            #---------- Screen for new users -------------#

def newUser():
    createLogin = input("Create login name: ")

    if createLogin in users: # check if login name exists
        print ("\nLogin name already exists!\n")
    else:
        createPassw = input("Create password: ")
        users[createLogin] = createPassw # add login and password
        print("\nAccount created!\n")     

                            #---------- Screen for old users -------------#

def oldUser():
    global login

    login = input("Enter login name: ")
    passw = input("Enter password: ")

    # check if user exists and login matches password
    if login in users and users[login] == passw:
        print("\nLogin successful!\n")
        mainMenu()

    else:
        print("\nUser doesn't exist or wrong password!\n")



# Main -------------------------------------------------------------------------------------------------------------


while status != "q":
    status = displayMenu()




Your code is working exactly as it is designed.您的代码完全按照设计工作。

The main loop starts:主循环开始:

while status != "q":
    status = displayMenu()

Note that your program will only ever "quit" if the user gives an input of "q" in the displayMenu() , rather than "Q" .请注意,只有当用户在displayMenu()输入"q"而不是"Q" ,您的程序才会“退出"Q" This can be fixed by checking status.lower() instead.这可以通过检查status.lower()来解决。

Then you specify that no, you are not a registered user by providing 2 as an input to the displayMenu() .然后通过向displayMenu()提供2作为输入来指定不,您不是注册用户。

This brings you to newUser() , where you create your new account.这会将您带到newUser() ,您可以在其中创建新帐户。

You'll then get returned to displayMenu() , where you can this time select 1 to login as a registered user, bringing you to oldUser() .然后您将返回displayMenu() ,这次您可以选择1以注册用户身份登录,将您带到oldUser()

Once you enter your login credentials in oldUser() , you'll get brought to mainMenu() , where it will print the welcome message in mainMenu() , then return to oldUser() , then to displayMenu() , then back to the main loop.一旦你输入你的登录凭据oldUser()你会得到带到mainMenu()它会打印在欢迎信息mainMenu()然后返回到oldUser()然后displayMenu()然后回主循环。

Unless you're expecting something else to happen in mainMenu() , the only error here is that you're not normalizing the case of status before checking if it is "q" .除非您期望在mainMenu()发生其他事情,否则这里唯一的错误是您没有在检查status是否为"q"之前对其进行规范化。

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

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