尝试 3 次的 Python 用户名和密码

0 python loops while-loop

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

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
Run Code Online (Sandbox Code Playgroud)

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

Meg*_*ets 6

修复了代码以实现您要执行的操作:

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
Run Code Online (Sandbox Code Playgroud)

已作出的更改:

  • 删除了usernameand的定义,password因为它是多余的,可以省略
  • while语句更改为计算 3 次迭代count
  • 仅在if声明中而不是在声明中验证凭据while
  • 将减少更改count增加(从count -=count +=
  • break 输入正确凭据时的循环