While loop to maximise password attempts – Python 3

Question:

I’m practising my coding – pretty new to this – and i’m trying to create a login system, all is working well so far but i’m trying to use a while loop to maximise the number of user attempts at inputting the correct password. Even if the user inputs the correct password, the loop will carry on until the loop is completed and will continue to say carry on.

login = {
    "alex": "dog123"
}
passkey = login.values()
correct_user = True


while correct_user:
    user = input("Please input your username ")
    if user in login:
        print(f'Hello {user}, please input your password.')
        correct_user = False
    else:
        print("Invalid user")

attempt = 0
max_attempts = 3
correct_password = True

while correct_password:
    password = input(">")
    if password in passkey:
        print(f"""Welcome back {user}! 
How can i help you today?""")
        correct_password = False
    else:   
        while attempt < max_attempts:
            print("try again")
            attempt += 1
            password = input(">")
            if password in passkey:
                correct_password = False
        else:
            print("too many guesses!")
            break
Asked By: alex

||

Answers:

Try this.

login = {
    "alex": "dog123"
}
passkey = login.values()
correct_user = True


while correct_user:
    user = input("Please input your username ")
    if user in login:
        print(f'Hello {user}, please input your password.')
        correct_user = False
    else:
        print("Invalid user")

attempt = 0
max_attempts = 3
correct_password = True

while correct_password:
    if attempt < max_attempts: # checks if an attempt is left.
        password = input(">")
        if password == login[user]: # Check password equal current userpassword.
            print(f"""Welcome back {user}! 
    How can I help you today?""")
            correct_password = False
        else:
            print("Invalid password")
            attempt += 1
    else: # print too many attempts 
        print("Too many attempts")
        correct_password = False



Don’t use password in passkey to check that if password is correct or not. Why?

Explanation

Let say you have more than one users

login = {
    "alex": "dog123",
    "alice": "cat123"

}

If you use in and check that if password is in passwords list then you face a bug

When

  1. Example
  • If user is equal alex and password is cat123 then you code don’t print that password is incorrect.

So use == and check if password == login[user] here user and password are the one that user enters.


"@alex comments. what does the ‘[user]’ aspect mean?".

@alex if you want all values from the dictionary then you use dict.values(), But if you want a specific key’s value then you use dict['Key'] or dict.get('Key'). Let’s say user='alex' then login[user] gives the value of the user from the login dict which is 'dog123'. Note if the user is not in the dict then it will give you a KeyError. But if you use dict.get(user) it will return the value of user or None if user is not in dict

Answered By: codester_09

Your error comes from breaking out of the inner while loop but still having the outer one running; it’s better to use only one loop (and you don’t need the ‘correct_password’ flag any more):

attempt = 0
max_attempts = 3
passkey = ['sesame']

while attempt < max_attempts:
    password = input(">")
    if password in passkey:
        print(f"""Welcome back {user}! How can i help you today?""")
        break
    else:
        attempt += 1
        if attempt < max_attempts:
            print("try again")    
else:
    print("too many guesses!")

As a side note, codester_09 is right about the way you check the password, so implement their modification too.

Answered By: Swifty
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.