Python Coding Exercise #34: The Elder
Part of the series Python Practice Exercise — part 34
Before you start: Know input() and int() · Know if, elif and else
The question
Please write a program which asks for the names and ages of two persons. The program should then print out the name of the elder.
Some examples of expected behaviour:
Person 1:
Name: Alan
Age: 26
Person 2:
Name: Ada
Age: 27
The elder is Ada
Person 1:
Name: Bill
Age: 1
Person 2:
Name: Jean
Age: 1
Bill and Jean are the same age
My Solution
print("Person 1:")
name1 = input("Name: ")
age1 = int(input("Age: "))
print("Person 2:")
name2 = input("Name: ")
age2 = int(input("Age: "))
if age1 > age2:
print(f"The elder is {name1}")
elif age2 > age1:
print(f"The elder is {name2}")
elif age1 == age2:
print(f"{name1} and {name2} are the same age")
How My Solution Works
1. Four pieces of information
The program needs a name and an age for two people, so it stores four values. The variable names name1, age1, name2 and age2 keep the pairs clear: the 1 goes with person 1, and the 2 with person 2.
The two kinds of value are converted differently:
- The names stay as strings, exactly as typed. A name isn’t a number.
- The ages are converted with
int(), because we need to compare them as numbers.
2. Compare the ages, print the names
This is the heart of the exercise. The program compares the ages but prints the names:
if age1 > age2:is true when person 1 is older, so it printsname1.elif age2 > age1:is true when person 2 is older, so it printsname2.elif age1 == age2:is true when the ages are equal, so it prints a message with both names.
Python checks the three conditions from top to bottom, and runs only the first one that’s true. This is the same if / elif pattern as in Exercise #33, where the numbers were compared directly. Here the numbers are the ages, and what gets printed is the name that belongs to the winning age.
Here is how the two examples go through the program:
| Person 1 | Person 2 | age1 > age2? | age2 > age1? | age1 == age2? | Printed |
|---|---|---|---|---|---|
| Alan, 26 | Ada, 27 | no | yes | (skipped) | The elder is Ada |
| Bill, 1 | Jean, 1 | no | no | yes | Bill and Jean are the same age |
University Solution
The University Solution is the same as mine, apart from the last branch, which is a plain else:
print("Person 1:")
name1 = input("Name: ")
age1 = int(input("Age: "))
print("Person 2:")
name2 = input("Name: ")
age2 = int(input("Age: "))
if age1 > age2:
print(f"The elder is {name1}")
elif age2 > age1:
print(f"The elder is {name2}")
else:
print(f"{name1} and {name2} are the same age")
When Python reaches the else, person 1 is not older, and person 2 is not older, so the only possibility left is that the ages are equal. The extra condition age1 == age2 in My Solution checked something that was already certain. Both programs print the same output for every pair of ages.
Two mistakes to avoid
Comparing the names instead of the ages
It’s easy to write the comparison with the wrong variables:
if name1 > name2:
print(f"The elder is {name1}")
This is perfectly valid Python, because strings can be compared too. But Python compares strings alphabetically, so "Alan" > "Ada" is True (the letters match until the second letter, and l comes after d). For the first example, this version prints The elder is Alan, which is wrong. Alan is 26 and Ada is 27. It would also say that Bill and Jean are not the same age.
The program has no error, so the only way to notice is to check the output. Remember: compare the thing the question is about, which is the age, and only use the name to show the answer.
Leaving the ages as text
What if the ages aren’t converted with int()?
age1 = input("Age: ")
age2 = input("Age: ")
Then the ages are strings, and they are compared character by character, like words in a dictionary. For small ages of the same length, this happens to work: "26" < "27". But it breaks as soon as the lengths differ:
print("9" > "10") # True, because "9" comes after "1" alphabetically
print(9 > 10) # False
So for a 9-year-old and a 10-year-old, the text version says the 9-year-old is the elder. And for ages 100 and 99, it says 99 is the elder. The mistake can hide for a long time, because it only shows up for certain ages. This is why input() results should always be converted before being compared as numbers. (See Exercise #08.)
Key takeaways
- Store each person’s name and age in separate, clearly named variables:
name1,age1,name2,age2. - Names stay as strings. Convert ages with
int()so they are compared as numbers. - Compare the ages, and print the names.
if/elif/elsechecks the conditions from top to bottom and runs only the first true one. A finalelsecan replace a lastelifwhen only one possibility remains.- Strings are compared alphabetically.
"Alan" > "Ada"isTrue, and"9" > "10"isTrue. - Test with ages of different lengths, such as 9 and 10, and with equal ages.
✦ 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.