Python Coding Exercise #46: Repeat Password
Part of the series Python Practice Exercise — part 46
Before you start: Know input() · Know while True and break · Know the == operator
The question
Please write a program which asks the user for a password. The program should then ask the user to type in the password again. If the user types in something else than the first password, the program should keep on asking until the user types the first password again correctly.
Have a look at the expected behaviour below:
Password: sekred
Repeat password: secret
They do not match!
Repeat password: cantremember
They do not match!
Repeat password: sekred
User account created!
Before coding: which lines repeat?
With loops, the first thing to decide is which lines are repeated and which are not. In this exercise:
| Step | How many times? |
|---|---|
| Ask for the password | Once, at the start |
| Ask to repeat the password | Again and again, until it matches |
Print They do not match! | Every time the repeat is wrong |
Print User account created! | Once, at the end |
So only the repeat question, and the check that follows it, go inside the loop. The first question stays outside. If the first question were inside the loop, the user would have to type a new password after every mistake.
My Solution
password = input("Password: ")
while True:
confirm_password = input("Repeat password: ")
if confirm_password == password:
break
print("They do not match!")
print("User account created!")
How My Solution Works
1. The password is asked once, before the loop
password = input("Password: ") runs one time and stores what the user typed. The loop never changes this variable, so it holds the password to compare against throughout.
2. The loop asks for the repeat each round
Inside while True: (see Exercise #43), confirm_password = input("Repeat password: ") runs at the start of every round. Each round replaces confirm_password with the new attempt.
3. A match leaves the loop with break
if confirm_password == password: checks whether the two are the same. If they are, break ends the loop immediately.
4. A mismatch prints the message and tries again
If the two are different, the if is false, and Python moves on to the next line: print("They do not match!"). Then the loop returns to the top and asks again.
This works because of where break is. When the passwords match, break jumps out of the loop before the print("They do not match!") line, so the message is never shown for the correct attempt.
5. User account created! is outside the loop
It isn’t indented, so it runs once, after the loop has ended.
Following the sample
| Step | The user types | confirm_password == password? | What happens |
|---|---|---|---|
| 1 | sekred (for Password:) | — | Stored in password |
| 2 | secret | No | Prints They do not match! |
| 3 | cantremember | No | Prints They do not match! |
| 4 | sekred | Yes | break |
| 5 | Prints User account created! |
If the user repeats the password correctly the first time, the program prints only User account created!, with no They do not match! before it.
University Solution
password = input("Password: ")
while True:
password_again = input("Repeat password: ")
if password == password_again:
break
print("They do not match!")
print("User account created!")
The University Solution is the same program as My Solution, with three small differences that change nothing in how it works:
- A different variable name:
password_againinstead ofconfirm_password. - The comparison is written the other way round:
password == password_again, where I wroteconfirm_password == password. The==operator gives the same answer either way. - A blank line before the last
print(), to separate the loop from the line after it.
Things to notice
The match has to be exact. == on strings is case-sensitive, as in Exercise #37. If the password is Secret, then secret doesn’t match, and neither does Secret with a space at the end. The program says They do not match! and asks again.
The first password is never checked. Whatever the user types first is accepted, including an empty password. The exercise only checks that the repeat is the same.
The password is visible when typed. input() shows everything on the screen as the user types. That’s fine for practising, but a real program would use a tool that hides the password while it’s typed.
Key takeaways
- With loops, first decide which lines repeat and which run once.
- A question that is asked once goes before the loop. A question that is repeated goes inside it.
- A variable set before the loop, like
password, keeps its value in every round. - The
ifwithbreakcomes before the “wrong” message, so the message is skipped when the answer is correct. ==on strings is exact, so capital letters and spaces matter.- Two variable names or two orderings of
==can give the same program.
✦ This is part of my Python Practice Exercise series, where I work through exercises while learning Python and explain each answer in plain language. ✦
Practice Source: This exercise is taken from the Python Programming MOOC 2026 by the University of Helsinki. I’m working through the exercises as part of my Python learning journey and documenting my answers and explanations here.