如何在Python中循环此登录?

2024-04-20 12:58:06 发布

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

我如何循环我的登录,以便每当有人用错误的用户名和密码登录时,程序将再次询问用户名和密码,直到它是正确的。在

def login():
    global Account
    UserName_File = 'users.csv'
    Separation = ','

    print('Welcome to the eHealth program, please sign in')
    UserName = input('Fill in your username: ')
    Password = input('Fill in your password: ')
    File = open(UserName_File, 'r')

    Signedin = False
    while Signedin != True:
        Account = File.readline()
        Account = Account.split(Separation)
        Account[-1] = Account[-1].strip('\n')
        print(Account)
        if Account[0] == '': #If the .csv file is empty or when the username and password are wrong 
            print('Failed to sign in, please try again')
            File.close()
            return
        else:
            UserName_File = Account[0]
            Password_File = Account[1]
            User = Account[2] #Doctor or patient
            if UserName_File == UserName and Password_File == Password and User == 'patient':
                Signedin = True
                print('Succesfully logged in, welcome patient')
                File.close()
                return
                #start to run patient functions
            elif UserName_File == UserName and Password_File == Password and User == 'doctor':
                Signedin = True
                print('Succesfully logged in, welcome doctor')
                File.close()
                return
                #start to run doctor functions

Account = ''
login()
print(Account)

在用户.csv包含

^{pr2}$

Tags: andcsvthetointrueclosereturn
1条回答
网友
1楼 · 发布于 2024-04-20 12:58:06

要使当前代码正常工作,只需将input部分移到while循环中,并删除return调用。固定代码:

def login():
    global Account
    UserName_File = 'users.csv'
    Separation = ','

    print('Welcome to the eHealth program, please sign in')

    Signedin = False

    while Signedin != True:

        UserName = input('Fill in your username: ')
        Password = input('Fill in your password: ')
        File = open(UserName_File, 'r')
        Account = File.readline()
        Account = Account.split(Separation)
        Account[-1] = Account[-1].strip('\n')
        print(Account)
        if Account[0] == '': #If the .csv file is empty or when the username and password are wrong 
            print('Failed to sign in, please try again')
            File.close()
        else:
            UserName_File = Account[0]
            Password_File = Account[1]
            User = Account[2] #Doctor or patient
            if UserName_File == UserName and Password_File == Password and User == 'patient':
                Signedin = True
                print('Succesfully logged in, welcome patient')
                File.close()
                #start to run patient functions
            elif UserName_File == UserName and Password_File == Password and User == 'doctor':
                Signedin = True
                print('Succesfully logged in, welcome doctor')
                File.close()
                #start to run doctor functions


Account = ''
login()
print(Account)

return将始终退出正在执行的函数,而不是循环。在

相关问题 更多 >