Python用户名和密码,共3次尝试

时间:2017-11-09 12:52:22

标签: 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

语法错误,无法在第6行上分配给操作员username = input。

4 个答案:

答案 0 :(得分:2)

这里试试这个(我尽量少改变你的代码,以便你自己识别相同的逻辑)

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

# "" or '' because you are assigning a value string into it
password = ""
username = ""

# looping will continue when wrong input for three times and ask again...
while password!='Hytu76E' and username!='bank_admin' and count < 3:
    # you are collecting user input from CLI separately (you can not assign and operator to such operation as per your code ;)
    username = input("Enter username: ")
    password = input("Enter password: ")

    if password=='Hytu76E' and username=='bank_admin':
     # if match, grand and break
     print('Access granted')
     break

    else:
        print('Access denied. Try again.')
        count+=1     # as per gbse, in the comments, you will need the + to count up

代码中的问题:

# you are assigning string value, what for? this would make the loop hit positive the first time
password=Hytu76E       # string assignment error in syntax, anyway
username=bank_admin    # string assignment error in syntax, anyway

# you can not assigning and operator in the input because of no if condition in this line, also you should compare the values of the input
username=input('Enter username: ') and password=input('Enter password: ')

# if code is ok, then move outside the loop in the case when the user enters the first time good answers
if password=='Hytu76E' and username=='bank_admin':
   print('Access granted')

    else:
        print('Access denied. Try again.')

        # you are decremented the counter which would never leave teh loop at 4, you should add one on each iteration so count+=1 (count = count + 1) 
        count-=1

答案 1 :(得分:2)

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

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输入正确凭据时的循环

答案 2 :(得分:0)

我认为这就是您正在寻找的内容:接受用户名和密码,并根据代码中提到的特定内容进行验证,最大限制为3

print('Enter correct username and password combo to continue')
count=1

while count<4:
    username=input('Enter username: ')
    password=input('Enter password: ')
    if password=='Hytu76E' and username=='bank_admin':
        print('Access granted')
        count=5
    else:
        print('Access denied. Try again.')
        count+=1

答案 3 :(得分:0)

首先,您可以删除在开始时为密码和用户名提供的初始定义,以及将while循环更改为while count&lt; 4

所以它看起来像:

print('enter the correct username and password combo to continue')
count = 0
while count<4:

如果我们保留了它以前的状态,那就更不必要了,并使你的程序更加混乱。

要修复语法错误,您需要删除并放置在用户名和密码之间,所以中间看起来更像是这样:

username = input('Enter username: ')
password = input('Enter password: ')

然后在结束时你想要将count- = 1改为count + = 1,因为如果它每次都不会达到4而你的循环将是无限的,这不是你想要实现的。

以下是整个修复:

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

以下是我所做的更改列表:

  • 删除了第3行和第4行中的密码和用户名定义

  • 将while循环更改为while&lt; 4

  • 删除了&inbetween username = input和password = input

  • 在if语句后添加count = 5,以便循环结束