Q:

Program keeps repeating itself. (Python)

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.

A:

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.

A:

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!

