我一直在尝试一些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循环创建了一个无限循环,但我找不到任何好的方法来结束它。
发布于 2016-12-08 23:09:01
你可以这样做
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")发布于 2016-12-08 23:15:40
在我朋友的帮助下解决了:
#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")发布于 2016-12-08 23:50:26
结束循环的一个好方法是使用for或计数器变量
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。
https://stackoverflow.com/questions/41042571
复制相似问题