Python - 20

Python - 20: Mini Project - Password Checker

Combine input, conditions, and a while loop to build a simple password checker with limited attempts.

Your challenge: Test the password checker, then customize the password, number of attempts, and messages.

✦ Python Editor
▶ Output

Why the old version got stuck

The starter code left two important lines unfinished: break and attempts = attempts - 1.

Because attempts stayed at 3, the condition while attempts > 0 stayed true forever. That caused the browser to keep opening the password prompt.

Reduce the attempts

After every incorrect password, this line moves the loop closer to stopping:

attempts = attempts - 1

The values now go from 3 to 2, then 1, then 0.

Stop after the correct password

break immediately exits the loop when the password is correct.

Lock the user out

After three incorrect attempts, attempts reaches 0 and the program prints LOCKED OUT!.

Try it: Test one correct password, three incorrect passwords, and then change the number of attempts to 5.

Good Python habit: When using a while loop, make sure every possible path either changes the loop condition or uses break. This prevents accidental infinite loops.

Note: This is a practice example for learning Python logic. Real applications should not store passwords directly inside source code.

👀 Show Example Solution
correct_password = "python123"

attempts = 3

while attempts > 0:

    password = input(
        "Enter password (" + str(attempts) + " tries left): "
    )

    if password == correct_password:
        print("ACCESS GRANTED")
        break

    else:
        attempts = attempts - 1

        print("ACCESS DENIED")
        print("Attempts left:")
        print(attempts)

if attempts == 0:
    print("LOCKED OUT!")

The important change is that the starter code now includes both loop-control lines, so the page cannot become trapped simply because the learner has not filled in the exercise yet.