【问题标题】:Dual password authentication in PythonPython中的双重密码认证
【发布时间】:2016-12-08 15:05:08
【问题描述】:

我在学校学到了一些东西,所以一直在尝试一些 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 循环创建了一个无限循环,但我找不到任何好的方法来结束它。

【问题讨论】:

    标签: python-3.x


    【解决方案1】:

    你可以做类似的事情

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

    【讨论】:

    • 感谢朋友的帮助,实际上我只是在朋友的帮助下才弄明白的。问题是当我执行 while 循环时,我没有重新分配 passcheck 变量。就是这样:#online auth email = input('你的电子邮件地址是什么?') password = input('输入密码') passcheck = input('请重新输入密码') while passcheck != 密码: passcheck = input('请重新输入密码以进行确认') else: print("确认码已通过电子邮件发送给您") 但是谢谢兄弟,我会看看你的代码以及它是如何工作的:)
    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-08-28
        • 2013-11-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-09-18
        • 2015-04-03
        相关资源
        最近更新 更多