← All coding practice

Coding Practice · Beginner · October 8, 2026 · 4 min read

Python Coding Exercise #15: Order of Magnitude

Part of the series Python Practice Exercise — part 15

Before you start: Know input() and int() · Know if statements and comparison operators

The question

Please write a program which asks the user for an integer number. The program should then print out the magnitude of the number according to the following examples.

Please type in a number: 950
This number is smaller than 1000
Thank you!
Please type in a number: 59
This number is smaller than 1000
This number is smaller than 100
Thank you!
Please type in a number: 2
This number is smaller than 1000
This number is smaller than 100
This number is smaller than 10
Thank you!
Please type in a number: 1123
Thank you!

My Solution

number = int(input("Please type in a number: "))
if number < 1000:
    print("This number is smaller than 1000")
if number < 100:
    print("This number is smaller than 100")
if number < 10:
    print("This number is smaller than 10")
print("Thank you!")

How My Solution Works

1. Converting the input to an integer (type casting)

The first line, number = int(input("Please type in a number: ")), uses type casting, which means converting a value from one type to another. The function input() always gives back a string, even when the user types digits, so "950" is text and not a number. The function int() converts that string into the integer 950, so the program can compare it with 1000, 100 and 10.

Without int(), the first comparison would stop the program with an error:

TypeError: '<' not supported between instances of 'str' and 'int'

Converting input is explained in Exercise #08, and type casting in Exercise #31 and Data Types in Python.

2. Three separate questions

The program contains three independent if statements, and each one asks its own question about the number. Python checks all three, one after another, and prints a line for every question whose answer is yes.

Here is what happens for the four examples:

Number< 1000?< 100?< 10?Lines printed before “Thank you!“
950yesnono1
59yesyesno2
2yesyesyes3
1123nonono0

That’s why a small number prints more lines than a big one. The number 2 is smaller than 10, and smaller than 100, and smaller than 1000, so all three messages are true.

3. “Thank you!” is not indented

print("Thank you!") starts at the left edge, so it doesn’t belong to any if. It runs every time, after the three checks. Indentation is how Python knows which lines belong to an if and which don’t.

University Solution

number = int(input("Please type in a number: "))
if number < 1000:
    print("This number is smaller than 1000")

if number < 100:
    print("This number is smaller than 100")

if number < 10:
    print("This number is smaller than 10")

print("Thank you!")

The University Solution is the same as My Solution. The only difference is the blank line it leaves between each if block, which makes the three separate questions easier to see. Blank lines don’t change what the program does.

Why not use elif?

When you have several conditions in a row, elif (“else if”) is often the right tool. But it works differently. With if / elif, Python stops at the first condition that’s true and skips the rest. Here’s the same idea written with elif, checking the smallest limit first:

number = int(input("Please type in a number: "))
if number < 10:
    print("This number is smaller than 10")
elif number < 100:
    print("This number is smaller than 100")
elif number < 1000:
    print("This number is smaller than 1000")
print("Thank you!")

For the input 59, this prints only:

This number is smaller than 100
Thank you!

That’s wrong for this exercise, because the expected output also includes “smaller than 1000”. The elif chain stopped after the first match.

So the rule of thumb is:

  • Use separate ifs when each question stands on its own and more than one answer can be “yes”.
  • Use if / elif / else when you want to pick exactly one option.

Edge cases

Because the comparisons use < (“smaller than”), the limits themselves don’t count:

  • 1000 prints only “Thank you!”, since 1000 is not smaller than 1000.
  • 10 prints the 1000 and 100 messages, but not the 10 one.
  • 0 and negative numbers are smaller than everything, so they print all three messages.

Key takeaways

  • input() returns a string, so convert it with int() (type casting) before comparing it with numbers.
  • Separate if statements are each checked on their own, so several of them can run.
  • if / elif stops at the first true condition. Use it only when you want exactly one result.
  • A line with no indentation after an if runs every time.
  • < excludes the limit itself. 1000 is not smaller than 1000.

✦ 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.

Tagged: python · conditionals · if-statements · comparison-operators