Python 用户名和密码,尝试 3 次 [英] Python username and password with 3 attempts

查看:39
本文介绍了Python 用户名和密码,尝试 3 次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

刚开始使用 python 并为此绞尽脑汁,但似乎无法正确解决.

Just started python and racking my brains on this but can't seem to get it right.

print('Enter correct username and password combo to continue')
count=0
password=Hytu76E
username=bank_admin

while password!='Hytu76E' and username!='bank_admin' and count<4:
    username=input('Enter username: ') and password=input('Enter password: ')

    if password=='Hytu76E' and username=='bank_admin':
     print('Access granted')

    else:
        print('Access denied. Try again.')
        count-=1

语法错误,无法分配给第 6 行 username=input 的运算符.

syntax error, can't assign to operator on line 6 username=input.

推荐答案

修复了代码以实现您正在尝试执行的操作:

Fixed the code to achieve what you are trying to do:

print('Enter correct username and password combo to continue')
count=0
while count < 3:
    username = input('Enter username: ')
    password = input('Enter password: ')
    if password=='Hytu76E' and username=='bank_admin':
        print('Access granted')
        break
    else:
        print('Access denied. Try again.')
        count += 1

已经做出的改变:

  • 删除了usernamepassword的定义,因为它是多余的,可以省略
  • 更改了 while 语句以计算 count
  • 的 3 次迭代
  • 仅在 if 语句中而不是在 while
  • 中验证凭据
  • count的减少改为增加(从count -=改为count +=)
  • break 输入正确的凭据时循环
  • Removed the definition of username and password since it is redundant and can be omitted
  • Changed the while statement to count 3 iterations of count
  • Validation of the credentials only in the if statement and not in the while
  • Changed the decreasing of count to increasing (from count -= to count +=)
  • break the loop when the right credentials are entered

这篇关于Python 用户名和密码,尝试 3 次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆