[英]Dual password authentication in Python
Been trying out some python as I learnt a bit in school and I've been trying to make a simple login system with password authentication. 我在学校里学到了一些python,我一直在尝试用密码认证创建一个简单的登录系统。
The way I want it to work: get username and password, ask for password for authentication and if password is not correct then ask again until it gets the right one 我希望它工作的方式:获取用户名和密码,请求密码进行身份验证,如果密码不正确,请再次询问,直到找到正确的密码
this is my code so far: 到目前为止这是我的代码:
#online auth
email = input('What is your email address?')
password = input('Enter a password')
passcheck = input('Please re-enter password for confimation')
while passcheck != password:
input('Please re-enter password for confirmation')
else: print("A confirmation code has been emailed to you")
When I run the program it asks for the email and username properly and then I get the confirmation question. 当我运行该程序时,它会正确地询问电子邮件和用户名,然后我收到确认问题。 If i enter the same password as the one i inputted in the first place it proceeds to the else statement.
如果我输入的密码与我在第一个地方输入的密码相同,则进入else语句。 If i enter the wrong one it ends a never ending loop of reenter the password, even if i input the correct one.
如果我输入了错误的一个,它就会结束一个永不停止的重新输入密码的循环,即使我输入了正确的密码。
I know that the while loop created an infinite loop but I cant find any good way to end it. 我知道while循环创建了一个无限循环,但我找不到任何好方法来结束它。
You could do something like 你可以做点什么
email = input('What is your email address?')
password_input = False
while not password_input or pass_check != password:
password = input('Enter a password')
pass_check = input('Please re-enter password for confimation')
password_input = True
print("A confirmation code has been emailed to you")
Just solved with the help of my friend: 刚刚在朋友的帮助下解决了:
#online auth
email = input('What is your email address?')
password = input('Enter a password')
passcheck = input('Please re-enter password for confimation')
while passcheck != password:
passcheck = input('Please re-enter password for confirmation')
else: print("A confirmation code has been emailed to you")
a good way to end a loop is to either use a for, or a counter variable 结束循环的好方法是使用for或counter变量
for index in range(0,5):
password = input('Enter a password')
pass_check = input('Please re-enter password for confimation')
if pass_check == password :
print("A confirmation code has been emailed to you")
index=65532
else :
print("The passwords do not match, try again.")
if index != 65532 :
print("too many attempts, if you want to retry, please restart the program")
note : the 65532 is not special, it's just out of range, so you exit the for. 注意:65532不是特殊的,它只是超出范围,所以你退出for。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.