如何输入main()而不使其循环登录()

2024-10-02 22:33:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我是Python新手,我想问一下如何让我的代码工作。 在login()函数中,如果用户名和密码正确,log=True,则转到main()函数时,不定义log变量

然后我在main()函数中在线找到addlog=login(),如下所示

def main():
    log = login()
    if log == True:
        identifier = loginChoice()
        if identifier == 1:
            customerMain()
        if identifier == 2:
            adminMain()

它适用于那个些人,但对我来说,它进入了无休止的循环,不断地调用login()函数

下面是我的全部代码

def loginChoice():
    print("\n","Login as:")
    print("1. Customer")
    print("2. Admin")
    choice =int(input())
    if choice == 1:
        login()
        return choice
    if choice == 2:
        login()
        return choice

def login():
    print("\n", "Login menu")
    user = input("Username: ")
    passw = input("Password: ")
    fhand = open("userpassword.txt", "r")
    for line in fhand.readlines():
        us, pw = line.strip().split("\t")
        if (user == us) and (passw == pw):
            log = True
            print("Login successful!")
            return True        
        else:
            print ("Wrong username/password")
            return False

def main():
    if log == True:
        identifier = loginChoice()
        if identifier == 1:
            customerMain()
        if identifier == 2:
            adminMain()

谢谢你帮助我

登录菜单的屏幕截图在循环中

The screenshot of the login menu is in loop


Tags: 函数代码logtrueinputreturnifmain
1条回答
网友
1楼 · 发布于 2024-10-02 22:33:47

我修改了您的代码。这将正常工作
但是customerMian()和adminMain()函数没有定义

def loginChoice():
    print("\n","Login as:")
    print("1. Customer")
    print("2. Admin")
    choice =int(input())
    return choice

def login():
    print("\n", "Login menu")
    user = input("Username: ")
    passw = input("Password: ")
    fhand = open("userpassword.txt", "r")
    for line in fhand.readlines():
        us,pw = line.strip().split("\t")
        if (user == us) and (passw == pw):
            print("Login successful!")
            return True        
        else:
            print ("Wrong username/password")
            return False

if __name__ == '__main__':
    log = login()
    if log == True:
        identifier = loginChoice()
        if identifier == 1:
            customerMain()
        if identifier == 2:
            adminMain()

相关问题 更多 >