繁体   English   中英

Python中的双密码身份验证

[英]Dual password authentication in Python

我在学校里学到了一些python,我一直在尝试用密码认证创建一个简单的登录系统。

我希望它工作的方式:获取用户名和密码,请求密码进行身份验证,如果密码不正确,请再次询问,直到找到正确的密码

到目前为止这是我的代码:

#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")

当我运行该程序时,它会正确地询问电子邮件和用户名,然后我收到确认问题。 如果我输入的密码与我在第一个地方输入的密码相同,则进入else语句。 如果我输入了错误的一个,它就会结束一个永不停止的重新输入密码的循环,即使我输入了正确的密码。

我知道while循环创建了一个无限循环,但我找不到任何好方法来结束它。

你可以做点什么

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")

刚刚在朋友的帮助下解决了:

#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")

结束循环的好方法是使用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")

注意:65532不是特殊的,它只是超出范围,所以你退出for。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM