-1

What should I be using when I want the following program to, after 2 wrong attempts and one (last) correct attempt, not to lock the account?

I am using the following code for a program that asks the user for a password, if the password is correct it accepts the password, if not says "try again" and if there are 3 attempts it says account is locked, but my problem is that even if there are two wrong attempts and one correct attempt, it'll still say "account is locked". What should I be doing differently?

count = 0 
while True: 
    password = input("Password: ")
    count += 1
    if count == 3: 
        print("account locked")
        break 
    else:
        if password == 'blue':
            print("password accepted")
            break 
        else:
            print("wrong password, try again")
BrokenBenchmark
  • 18,126
  • 7
  • 21
  • 33
jackie321
  • 9
  • 2
  • 1
    You should be incrementing only if the password is wrong. Right now it counts all attempts, not just wrong ones. – shriakhilc Feb 01 '22 at 22:41
  • Your question is very similar to [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response), so I suggest you take a look at its answers. – martineau Feb 01 '22 at 23:09
  • You should be incrementing the count after the check — but before the end of the loop. The way you have it written, it will lock the account before the 3rd attempt is validated. – Ali Samji Feb 01 '22 at 23:16

4 Answers4

1

As the comment states, you should only be incrementing if the password is incorrect.

I do think, however, it's also worth pointing out that the use of while True + break can make your code harder to read, and is usually worth avoiding if you have some explicit condition you want to break on. That makes it easier for you and others to debug your code.

In this case, you only want to prompt for the password while the correct password hasn't been entered and three attempts haven't been used. So, you can make that condition explicit, like so:

count = 0
correct_password_entered = False
while not correct_password_entered and count < 3: 
    password = input("Password: ")
    if password == 'blue':
        print("password accepted")
        correct_password_entered = True
    else:
        print("wrong password, try again")
        count += 1

if count == 3:
    print('account locked')
BrokenBenchmark
  • 18,126
  • 7
  • 21
  • 33
0

Had a different approach to your problem. I also added a message saying how many more attempts are left before the account locks.

count = 3 
pwd = 'blue'
while count > 0: 
    count -= 1
    password = input("Password: ")
    if password == pwd:
        print("password accepted")
        break 
    else:
        print("wrong password, try again")
        print(f"You have {count} attempts left.")
    if count == 0:
        print("You have been locked out.")
Robin Sage
  • 969
  • 1
  • 8
  • 24
0

The other answers are correct and I'm not adding a "better" solution, just a different one, so if you accept an answer I think it should be one of them.

I'd just like to take this opportunity to show a different approach using the lesser known for-else construct, as I think this would be a perfect fit here and may be a bit more pythonic.

for i in range(3):
    password = input("Password: ")
    if password == "blue":
        print("password accepted")
        break
    else:
        print("wrong password, try again")
else:
    print("account locked")

The else part of a for loop will execute if the loop ran through without breaking.

Serge Hauri
  • 313
  • 1
  • 5
0

I also changed some parts in my 1st code, I think it should be alright now.

count = 1
while True: 
    password = input("Password: ")
    
    
    if password == '456':
     print('password accepted')
     break
    
    else:
     if count == 3:
      print('access locked')
      break
     else:
      print('wrong-try again')
      count += 1
jackie321
  • 9
  • 2