如何在Python中循环此登录?
如何循环登录,以便每当有人使用错误的用户名和密码登录时,程序将再次询问用户名和密码,直到正确为止如何在Python中循环此登录?,python,function,loops,csv,login,Python,Function,Loops,Csv,Login,如何循环登录,以便每当有人使用错误的用户名和密码登录时,程序将再次询问用户名和密码,直到正确为止 def login(): global Account UserName_File = 'users.csv' Separation = ',' print('Welcome to the eHealth program, please sign in') UserName = input('Fill in your username: ') Pas
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)
users.csv包含
username,password,function
patient,patient1,patient
doctor,doctor1,doctor
etc...
要使当前代码正常工作,只需将
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
将始终退出正在执行的函数,而不是循环。我希望这是学校作业或其他形式的玩具程序。在CSV文件中存储明文密码非常不安全。不幸的是,这将继续循环“登录失败,请重试”错误。@DamienBrils我在那里犯了很多愚蠢的错误,对不起。“我现在正在寻找一个合适的答案。”达米恩布里尔斯说,“我想现在应该可以了。”。对不起,我以前的回答很糟糕。