0
attemptsRemaining = 5


if attemptsRemaining == 0:
    print("Entry failed. Locking program.")
    exit()

while attemptsRemaining > 0:
    passwordEntry = input("Enter the password to access the data: ")

    if passwordEntry == 1234:
        print("test")

    else:
        attemptsRemaining -1

So, I'm using Python to write a simple password script, but the program doesn't stop looping the "enter the password" input even after I get it right, and when I get it wrong five times it still loops. Does anybody know how I can troubleshoot this issue?

Thanks.

4 Answers 4

4

There are 3 things wrong with your code.

First, you want to break the while loop after entering the correct password.

Second, you have a typo in your else clause: it should be -= 1 attemptsRemaining - 1 calculates the correct value, but does not assign it back to a variable.

The following code should work for you

attemptsRemaining = 5


if attemptsRemaining == 0:
    print("Entry failed. Locking program.")
    exit()

while attemptsRemaining > 0:
    passwordEntry = input("Enter the password to access the data: ")

    if passwordEntry == 1234: # if you get the password correct
        print("test") # print test
        break # and come out of the loop

    else:
        attemptsRemaining -=1

Thirdly, you are comparing the value of the user input against an integer. input() values are going to be stored as a string, so you are comparing different types that will always return False. You need to either convert passwordEntry to an int, or compare against '1234' the string.

Sign up to request clarification or add additional context in comments.

Comments

0

The problem is that you are not actually decrementing attemptsRemaining.
You need to do the equivalent of attemptsRemaining = attemptsRemaining - 1.

More commonly & more concisely, you can do attemptsRemaining -= 1.

The other issue you will find (and less obvious) is that when you are calling input, the value is going to be stored as a String. You are comparing against 1234 the integer, so it will always return False and never consider your password correct, even when it is 1234.

And finally, you need to make sure that you break out of your while loop when the password is correct. Otherwise, you'll be stuck in the loop!

Comments

0
attemptsRemaining = 5


while attemptsRemaining > 0:
    passwordEntry = input("Enter the password to access the data: ")

    if passwordEntry == 1234:
        print("test")
        break # exit loop

    else:
        attemptsRemaining -=1

if attemptsRemaining == 0:
    print("Entry failed. Locking program.")
    exit()

Comments

0

proper way to execute if statement also. and exit after 5 wrong attempts

attemptsRemaining = 5

while attemptsRemaining > 0:
    passwordEntry = input("Enter the password to access the data: ")

    if passwordEntry == "1":
        print("test")
        print (attemptsRemaining)
        break
    else:
        attemptsRemaining = attemptsRemaining - 1

    if attemptsRemaining == 0:
        print("Entry failed. Locking program.")
        exit()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.