Python Coding Exercise #32: Age of Maturity
Part of the series Python Practice Exercise — part 32
Before you start: Know input() and int() · Know comparison operators
The question
Please write a program which asks the user for their age. The program should then print out a message based on whether the user is of age or not, using 18 as the age of maturity.
Some examples of expected behaviour:
How old are you? 12
You are not of age!
How old are you? 32
You are of age!
My Solution
age = int(input("How old are you? "))
if age >= 18:
print("You are of age!")
else:
print("You are not of age!")
How My Solution Works
1. Read the age as a number
int(input("How old are you? ")) asks the question and converts the answer from text to a whole number. That conversion is needed, because you can’t compare the text "32" with the number 18. (See Exercise #08.)
2. One question, two answers
The program asks a single yes-or-no question: age >= 18. Python answers True or False, and the if / else picks one of two messages:
- If the answer is True, the indented line under
ifruns, and it printsYou are of age!. - If the answer is False, the
elsebranch runs instead, and it printsYou are not of age!.
Exactly one of the two messages is always printed. There’s no case where both appear or neither does.
3. Why >= and not >
The exercise says “using 18 as the age of maturity”, which means that someone who is exactly 18 is of age. So the condition must include 18, and that needs >= (“greater than or equal to”), not >.
The boundary values show it clearly:
| Age | age >= 18 | Printed |
|---|---|---|
| 12 | False | You are not of age! |
| 17 | False | You are not of age! |
| 18 | True | You are of age! |
| 19 | True | You are of age! |
| 32 | True | You are of age! |
The trap: the examples don’t catch the mistake
Imagine writing if age > 18: instead. Try it on the two examples from the exercise: age 12 is correctly “not of age”, and age 32 is correctly “of age”. Both examples pass, so the program looks right. But for exactly 18, it prints You are not of age!, which is wrong.
This is why it’s worth testing the boundary, the exact limit, and the values just below and above it (17, 18 and 19). The sample inputs of 12 and 32 are far from the limit, so they can’t reveal this mistake. (There’s more about testing boundaries in Exercise #20.)
Other ways to write it
Flip the condition. You can test for the opposite case first, with < (“less than”):
age = int(input("How old are you? "))
if age < 18:
print("You are not of age!")
else:
print("You are of age!")
This gives exactly the same output for every age. age < 18 and age >= 18 are opposites, so exactly one of them is true.
Store the answer in a variable. A comparison produces a value of type bool (True or False), so you can keep it in a variable with a clear name:
age = int(input("How old are you? "))
is_of_age = age >= 18
if is_of_age:
print("You are of age!")
else:
print("You are not of age!")
The if now reads almost like English: “if is of age”. This is a nice habit when the condition is long or used more than once. All three versions print the same results.
Key takeaways
if/elsepicks exactly one of two paths, depending on whether a condition isTrueorFalse.- “Of age at 18” includes 18 itself, so use
>=, not>. - A wrong boundary can pass the examples in the exercise. Always test the limit and the values on both sides of it: 17, 18 and 19.
age < 18andage >= 18are opposites, so flipping the condition (and swapping the branches) gives the same program.- A comparison gives a
bool, which you can store in a variable such asis_of_age. - Convert the input with
int()before comparing it with a number.
✦ 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.