Python Coding Exercise #36: Age Check
Part of the series Python Practice Exercise — part 36
Before you start: Know input() and int() · Know if, elif and else
The question
Please write a program which asks for the user’s age. If the age is not plausible, that is, it is under 5 or something that can’t be an actual human age, the program should print out a comment.
Have a look at the examples of expected behaviour below to figure out which comment is applicable in each case.
What is your age? 13
Ok, you're 13 years old
What is your age? 2
I suspect you can't write quite yet...
What is your age? -4
That must be a mistake
My Solution
age = int(input("What is your age? "))
if age >= 5:
print(f"Ok, you're {age} years old")
elif 0 <= age < 5:
print("I suspect you can't write quite yet...")
elif age < 0:
print("That must be a mistake")
How My Solution Works
1. Three groups of ages
Reading the examples, the ages fall into three groups, and each has its own message:
| Group | Ages | Message |
|---|---|---|
| Normal | 5 or more | Ok, you're 13 years old |
| Too young to write | 0 to 4 | I suspect you can't write quite yet... |
| Impossible | below 0 | That must be a mistake |
The three groups don’t overlap, and together they cover every whole number, so exactly one message is printed for any age.
2. A condition for each group
age >= 5covers the normal ages. It includes 5 itself, because only ages under 5 are “too young”.0 <= age < 5covers 0, 1, 2, 3 and 4.age < 0covers every negative number.
The middle condition is a chained comparison. In Python you can write 0 <= age < 5 to mean “age is at least 0 and less than 5”. It’s a short way to write age >= 0 and age < 5, and it reads like the maths notation 0 ≤ age < 5. For an age of 3, it’s true. For 7 or -1, it’s false.
3. Checking the boundaries
As in earlier exercises, the values at the edges of each group are the ones to test. Here are the ages around 0 and 5:
| Age | Group | Printed |
|---|---|---|
| -4 | impossible | That must be a mistake |
| -1 | impossible | That must be a mistake |
| 0 | too young | I suspect you can't write quite yet... |
| 4 | too young | I suspect you can't write quite yet... |
| 5 | normal | Ok, you're 5 years old |
| 13 | normal | Ok, you're 13 years old |
An age of exactly 0 counts as “too young”, not as a mistake, and an age of exactly 5 counts as normal. (More about checking limits in Exercise #20 and Exercise #32.)
University Solution
The University Solution checks the groups in a different order, and that makes the conditions simpler:
age = int(input("What is your age? "))
if age < 0:
print("That must be a mistake")
elif age < 5:
print("I suspect you can't write quite yet...")
else:
print("Ok, you're " + str(age) + " years old")
Python checks an if / elif / else chain from top to bottom, and stops at the first condition that’s true. That means that each later branch can rely on the earlier ones having failed:
- The first branch catches every negative age.
- By the time Python reaches
elif age < 5, the age can’t be negative any more, because a negative age would have been caught already. Soage < 5really means “from 0 to 4”. There’s no need for0 <=. - By the time Python reaches the
else, the age is 5 or more, so no condition is needed at all.
The result is the same as My Solution for every age from -60 to 160.
The last line also differs: it builds the text with + and str(age), while My Solution uses an f-string. Both give Ok, you're 13 years old. (More about str() and f-strings in Exercise #29.)
The trap: the order of the conditions
Because Python stops at the first true condition, the order matters. Look at what happens if the two “young” checks are swapped:
age = int(input("What is your age? "))
if age < 5:
print("I suspect you can't write quite yet...")
elif age < 0:
print("That must be a mistake")
else:
print(f"Ok, you're {age} years old")
This looks reasonable, and it works for 13 and for 2. But for -4, the first condition age < 5 is already true, because -4 is less than 5. Python prints I suspect you can't write quite yet... and never reaches the elif age < 0. The “That must be a mistake” message is unreachable, and every negative age gets the wrong comment.
The lesson: in an if / elif chain, put the most specific or most restrictive condition first. Here, age < 0 is narrower than age < 5, so it must come before it. Whenever you write a chain, ask for each branch: “can an earlier branch accidentally catch values that belong here?”
Taking it further
The exercise mentions ages that “can’t be an actual human age”, and the examples only show negative numbers. My Solution, like the University Solution, prints Ok, you're 500 years old for an age of 500. If you wanted to reject unrealistically high ages as well, you would add a branch for that, for example elif age > 120:, placed before the final normal case. Where you put it matters for the same reason as above.
Key takeaways
- When you can describe the possible inputs as separate groups, give each group its own branch.
0 <= age < 5is a chained comparison. It means “at least 0 and less than 5”.- Python runs only the first true branch of an
if/elif/elsechain, so the order of the branches matters. - Putting the narrowest condition first lets the later conditions be simpler, because they can rely on the earlier ones having failed.
- A wrong order can make a branch unreachable without any error message. Test values from each group, especially at the boundaries: -1, 0, 4 and 5.
- A final
elsecan replace a lastelifwhen only one group is left.
✦ 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.