Python Coding Exercise #33: Greater Than or Equal To
Part of the series Python Practice Exercise — part 33
Before you start: Know input() and int() · Know if and else
The question
Please write a program which asks for two integer numbers. The program should then print out whichever is greater. If the numbers are equal, the program should print a different message.
Some examples of expected behaviour:
Please type in the first number: 5
Please type in another number: 3
The greater number was: 5
Please type in the first number: 5
Please type in another number: 8
The greater number was: 8
Please type in the first number: 5
Please type in another number: 5
The numbers are equal!
My Solution
number1 = int(input("Please type in the first number: "))
number2 = int(input("Please type in another number: "))
if number1 > number2:
print(f"The greater number was: {number1}")
elif number2 > number1:
print(f"The greater number was: {number2}")
elif number1 == number2:
print("The numbers are equal!")
How My Solution Works
1. Two numbers
Both answers are converted with int(), so they can be compared as numbers. (See Exercise #08.)
2. Three possible outcomes
Two numbers can be related in exactly three ways: the first is bigger, the second is bigger, or they are equal. An if / elif / else chain is made for this kind of choice. Python checks the conditions from top to bottom and runs the first one that is true. The rest are skipped.
if number1 > number2:is true when the first number is bigger, so it printsnumber1.elif number2 > number1:is checked only if the first condition was false. It’s true when the second number is bigger, so it printsnumber2.elif number1 == number2:is checked only if both earlier conditions were false. It’s true when the numbers are equal, so it prints the “equal” message.
elif is short for “else if”. Here is how the three examples go through the chain:
| First | Second | number1 > number2? | number2 > number1? | number1 == number2? | Printed |
|---|---|---|---|---|---|
| 5 | 3 | yes | (skipped) | (skipped) | The greater number was: 5 |
| 5 | 8 | no | yes | (skipped) | The greater number was: 8 |
| 5 | 5 | no | no | yes | The numbers are equal! |
3. Why elif and not separate ifs
With elif, only one branch can ever run. In this exercise the three conditions are mutually exclusive anyway, so separate if statements would also give the right output. But elif says clearly that exactly one of these cases applies, and it avoids checking conditions that no longer matter. (Compare Exercise #15, where separate ifs were required, because several conditions could be true at once.)
University Solution
The University Solution is the same as mine, except for its last branch:
number1 = int(input("Please type in the first number: "))
number2 = int(input("Please type in another number: "))
if number1 > number2:
print("The greater number was:", number1)
elif number2 > number1:
print("The greater number was:", number2)
else:
print("The numbers are equal!")
The last elif number1 == number2: has become a plain else:. Think about what has to be true when Python reaches the end of the chain: the first number is not greater than the second, and the second is not greater than the first. The only possibility left is that they are equal. So the else is enough, and the extra condition number1 == number2 was checking something that is already certain. Both programs print the same output for every pair of numbers.
The University Solution also prints with a comma, print("The greater number was:", number1), which puts a space between the text and the number. My f-string version gives the same result. (See Exercise #05 for how commas and spaces work in print().)
Other ways to write it
Check for equal first. The order of the branches is up to you (compare Exercise #36, where it does matter), as long as every case is covered:
number1 = int(input("Please type in the first number: "))
number2 = int(input("Please type in another number: "))
if number1 == number2:
print("The numbers are equal!")
elif number1 > number2:
print(f"The greater number was: {number1}")
else:
print(f"The greater number was: {number2}")
This prints the same output as the other versions. Here the else means “the second number is greater”, because both other cases have already been ruled out.
Use max(). The built-in function max() from Exercise #23 finds the greater of two numbers directly:
print(max(5, 3)) # 5
But it can’t replace the whole program, because the exercise needs a different message when the numbers are equal, and max(5, 5) just gives 5. You still need an if to detect equality.
The trap: >= hides the equal case
The title of the exercise mentions “greater than or equal to”, so it’s tempting to use the >= operator:
if number1 >= number2:
print(f"The greater number was: {number1}")
else:
print(f"The greater number was: {number2}")
This version gives the right answer for 5 and 3, and for 5 and 8. But for 5 and 5, it prints The greater number was: 5, which is wrong, because the exercise wants The numbers are equal!. The equal case gets swallowed by the first branch, since 5 >= 5 is true.
The lesson: when a program has three distinct outcomes, give each one its own condition. And don’t test only the “normal” examples. Always try the case where the two values are equal.
Key takeaways
- Two numbers can be related in three ways: greater, smaller or equal. An
if/elif/elsechain handles all three. - Python checks the conditions from top to bottom and runs only the first one that’s true.
elifmeans “else if”. It’s only checked when the earlier conditions were false.- A final
elsecan replace a lastelifwhen only one possibility is left. >=includes the equal case, so it can hide the case that needs its own message.- Test with equal values as well as different ones.
✦ 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.