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

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

问题描述

刚开始使用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

已进行的更改:

  • 删除了 username password 的定义,因为它是多余的,可以省略
  • 更改了 while 语句以计算3次 count
  • 的迭代
  • 仅在 if 语句中而不在 while
  • 中验证凭据
  • count 的减少量更改为增加(从 count-= 更改为 count + = )
  • 输入正确的凭据后,
  • 打破循环
  • 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

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

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