Python username and password with 3 attempts

心不动则不痛 提交于 2019-12-23 05:23:13

问题


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

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


回答1:


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

Changes that have been made:

  • 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



回答2:


here try this (I try to change your code as less as possible so that you can identify the same logic yourself)

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

issues in your code:

# 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



回答3:


I think this is what you're looking for: Accept username and password and verify it against a particular one mentioned in the code, with a max try limit of 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



回答4:


Firstly you can remove the initial definition you gave to password and username at the start as well as changing the while loop to become while count<4

So it would look like:

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

If we had kept it how it was previously it would be unnecessary and clutter your program more.

To fix your syntax error you need to remove the and placed inbetween username and password, so the middle will look more like this:

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

Then at the end you want to change count-=1 to count+=1, because if it takes one away every time it will never hit 4 and your loop will be infinite, which is not what you are trying to achieve.

Here is the entire fix:

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

Here is a list of changes I have made:

  • Removed the password and username definition in lines 3 and 4

  • Changed your while loop to become while<4

  • Removed the and inbetween username=input and password=input

  • Added count=5 after if statement so that the loop ends



来源:https://stackoverflow.com/questions/47202331/python-username-and-password-with-3-attempts

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!