Python Coding Exercise #47: PIN and Number of Attempts
Part of the series Python Practice Exercise — part 47
Before you start: Know input() · Know while True and break · Know if, elif and else · Know f-strings
The question
Please write a program which keeps asking the user for a PIN code until they type in the correct one, which is 4321. The program should then print out the number of times the user tried different codes.
PIN: 3245
Wrong
PIN: 1234
Wrong
PIN: 0000
Wrong
PIN: 4321
Correct! It took you 4 attempts
If the user gets it right on the first try, the program should print out something a bit different:
PIN: 4321
Correct! It only took you one single attempt!
What is new here: a counter
Besides repeating, this program has to remember the number of attempts user makes. A variable that counts is called a counter. It follows three steps:
- Create it before the loop, with a starting value.
- Add 1 to it inside the loop, each time something happens.
- Use it after the count is finished, or when the program needs the number.
The adding is done with attempts += 1, which is the shorthand for attempts = attempts + 1. (The same style of shorthand was used in Exercise #29.)
Before coding: the PIN is text
The PIN is compared as text, not as a number: "4321" in quotes, not 4321. There are two reasons:
- A PIN can start with a zero, such as
0000in the sample. If it were converted withint(),0000would turn into0and the leading zeros would be lost. - A PIN is a code, not a quantity. We never add or subtract PINs, so there is no reason to convert it.
That’s why the program never uses int(), and why input() can be compared directly with "4321".
My Solution
attempts = 0
while True:
pin = input("PIN: ")
attempts += 1
if pin == "4321" and attempts == 1:
print("Correct! It only took you one single attempt!")
break
elif pin != "4321":
print("Wrong")
else:
print(f"Correct! It took you {attempts} attempts")
break
How My Solution Works
1. The counter starts at 0
attempts = 0 is written before the loop, so it runs once. At that point, the user hasn’t tried anything yet, so zero is the right starting value.
2. Each round asks, then counts
At the start of every round, the program asks for the PIN and immediately adds 1 to attempts. So by the time the checks run, attempts already includes the PIN the user just typed.
3. Three branches cover every case
Every PIN the user types is one of three things:
| Branch | Condition | When it runs | What happens |
|---|---|---|---|
if | pin == "4321" and attempts == 1 | Correct PIN on the first try | Prints the “single attempt” message, then break |
elif | pin != "4321" | Any wrong PIN | Prints Wrong, then the loop continues |
else | (everything left) | Correct PIN after more than one try | Prints the number of attempts, then break |
The else has no condition of its own, because by the time Python reaches it, the other two branches have been ruled out. The PIN can’t be wrong (the elif caught that), so it must be correct, and it can’t be the first attempt (the if caught that). So the else means: correct, after more than one attempt.
4. break is used twice
There are two places where the PIN is correct, so there are two break lines. The loop ends as soon as either runs. The wrong-PIN branch has no break, so the loop goes around again. (break is explained in Exercise #43.)
Following the sample
| Round | The user types | attempts after counting | Branch | Python prints |
|---|---|---|---|---|
| 1 | 3245 | 1 | elif | Wrong |
| 2 | 1234 | 2 | elif | Wrong |
| 3 | 0000 | 3 | elif | Wrong |
| 4 | 4321 | 4 | else | Correct! It took you 4 attempts, then break |
If the user types 4321 straight away, attempts is 1 and the first if is true, so the program prints Correct! It only took you one single attempt!.
University Solution
attempts = 1
while True:
pin = input("PIN: ")
if pin == "4321":
break
print("Wrong")
attempts += 1
if attempts == 1:
print("Correct! It only took you one single attempt!")
else:
print(f"Correct! It took you {attempts} attempts")
The University Solution gets the same results, but organises the work differently:
- The counter starts at 1, not 0. The first attempt is already “in progress” when the loop begins, so it is counted from the start.
- The count goes up only after a wrong PIN.
attempts += 1comes afterprint("Wrong"), so it runs only when the PIN was wrong. A correct PIN reachesbreakfirst, and the count stays right. - The loop only decides when to stop. It has one
ifand onebreak. The loop only asks, checks and saysWrong. - The final message is decided after the loop. The last
if/elseruns once, outside the loop, and chooses between the “single attempt” message and the count.
The main difference is where the decision about the message is made. My Solution decides inside the loop, so it needs three branches and two break lines. The University Solution decides after the loop, so the loop is shorter and the message logic is in one place.
Both programs print the same output for every sequence of PINs tested, including a correct PIN on the first try, after one wrong PIN, and after three wrong PINs.
Things to notice
The special message avoids “1 attempts”. Without the first-try case, the program would print It took you 1 attempts, which is bad grammar. That’s the reason the exercise asks for a different message when there is only one attempt.
A PIN must match exactly. "4321 " with a space at the end, or "04321", doesn’t equal "4321", so the program says Wrong. (The same exactness was explained in Exercise #37.)
Key takeaways
- A counter is a variable that starts before the loop and goes up by 1 inside it.
attempts += 1is the shorthand forattempts = attempts + 1.- A PIN is a code, not a quantity, so keep it as text. That way leading zeros stay.
- The starting value of a counter depends on where in the loop you add to it. Counting at the start of the round starts at 0, and counting only after a wrong answer starts at 1.
- An
elseruns when every earlier branch has been ruled out, so it needs no condition of its own. - A decision can be made inside the loop or after it. Deciding after the loop keeps the loop short.
✦ 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.