我如何循环我的登录,以便每当有人用错误的用户名和密码登录时,程序将再次询问用户名和密码,直到它是正确的。在
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}$
要使当前代码正常工作,只需将
input
部分移到while循环中,并删除return
调用。固定代码:return
将始终退出正在执行的函数,而不是循环。在相关问题 更多 >
编程相关推荐