尝试3次的Python用户名和密码 [英] Python username and password with 3 attempts
本文介绍了尝试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
andpassword
since it is redundant and can be omitted - Changed the
while
statement to count 3 iterations ofcount
- Validation of the credentials only in the
if
statement and not in thewhile
- Changed the decreasing of
count
to increasing (fromcount -=
tocount +=
) break
the loop when the right credentials are entered
这篇关于尝试3次的Python用户名和密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文