← All coding practice

Coding Practice · Beginner · October 21, 2026 · 6 min read

Python Coding Exercise #51: Even Numbers, Countdown and Counting Up

Part of the series Python Practice Exercise — part 51

Before you start: Know input() and int() · Know comparison operators · Know while True and break

This post combines three short exercises from the section “Loops with conditions” in Part 3 of the course. All three use the same new idea, so I put them together.

What is new here: a loop with a condition

In the last few exercises (Exercise #43 to Exercise #50), I used while True: and ended the loop with break. This section does it differently. The loop is given a condition, and Python checks it before every round:

while condition:
    # these lines repeat for as long as the condition is true

When the condition becomes false, the loop stops by itself. There is no break. For this to work, a loop like this needs three things:

  1. A starting value, set before the loop.
  2. A condition, which says when to keep going.
  3. A change inside the loop, so that the condition eventually becomes false.

If the change is missing, the condition never becomes false, and the loop never ends. All three exercises below follow this pattern.

Exercise 1: Print numbers

The question

Please write a program which prints out all the even numbers between two and thirty, using a loop. Print each number on a separate line.

The beginning of your output should look like this:

2
4
6
8
etc...

My Solution

number = 2
while number <= 30:
    print(number)
    number +=2

How My Solution Works

  • The starting value: number = 2 is the first even number.
  • The condition: while number <= 30: keeps going for as long as number is 30 or less. I used <= and not <, because 30 itself has to be printed.
  • The change: number +=2 adds 2 after each print, so the numbers go 2, 4, 6, and so on. Starting at an even number and adding 2 each time gives only even numbers.

The last round prints 30. Then number becomes 32, and the condition 32 <= 30 is false, so the loop stops. The output is the 15 even numbers from 2 to 30, one on each line.

Exercise 2: Fix the code: Countdown

The question

The program below has some syntactic issues:

print("Are you ready?")
number = int(input("Please type in a number: "))
while number = 0:
print(number)
print("Now!")

Please fix it so that it prints out the following:

Are you ready?
Please type in a number: 5
5
4
3
2
1
Now!

This exercise is similar to the countdown exercise in the last section, but please don’t use a while True loop this time round!

The problems

This program has four problems. Two of them are errors that Python reports, and two only show up as wrong behaviour:

#ProblemWhat happensThe fix
1while number = 0: uses =, which stores a value, instead of comparingSyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?Use a comparison operator
2print(number) is not indented, so it isn’t inside the loopIndentationError: expected an indented block after 'while' statement on line 3Indent it by four spaces
3The condition number == 0 is the wrong testFor 5, the loop is skipped and only Now! is printed. For 0, it prints 0 forever.number > 0
4Nothing changes number inside the loopWith number > 0, it prints 5 forevernumber -= 1

(The wording of error messages can differ slightly between Python versions.)

Python only reports problems 1 and 2, and it shows them one at a time, so it takes two runs to find both. Problems 3 and 4 are logic errors. The program runs, but gives the wrong result. This is the same kind of mistake as in Exercise #45.

My Solution

print("Are you ready?")
number = int(input("Please type in a number: "))
while number > 0:
    print(number)
    number -= 1
print("Now!")

How My Solution Works

  • The starting value is the number the user typed.
  • The condition number > 0 means: keep going while there are still numbers left to print.
  • The change is number -= 1, the shorthand for number = number - 1. It makes the number one smaller after each print.
  • print("Now!") is not indented, so it runs once, after the loop ends.
Roundnumber at the checknumber > 0?Python printsnumber after
15Yes54
24Yes43
33Yes32
42Yes21
51Yes10
—0Nothe loop stops

Then Now! is printed. This is the same countdown as in Exercise #45, but this time the condition ends the loop, so no break is needed. If the user types 0 or a negative number, the condition is false at the very first check, and the program prints only Are you ready? and Now!.

Exercise 3: Numbers

The question

Please write a program which asks the user for a number. The program then prints out all integer numbers greater than zero but smaller than the input.

Upper limit: 5
1
2
3
4

Please don’t use the value True as the condition of your while loop in this exercise!

My Solution

number = int(input("Upper limit: "))
sequence = 1
while sequence < number:
    print(sequence)
    sequence += 1

How My Solution Works

  • The starting value: sequence = 1, because the numbers must be greater than zero.
  • The condition: sequence < number uses <, not <=, because the numbers must be smaller than the input. The input itself is not printed.
  • The change: sequence += 1 moves to the next number.

For an upper limit of 5, the loop prints 1, 2, 3 and 4. When sequence becomes 5, the condition 5 < 5 is false, so 5 is never printed.

Things to notice

< and <= decide whether the last number is included. Exercise 1 had to print 30, so it used <=. Exercise 3 had to stop before the limit, so it used <.

ConditionLast number printed for a limit of 5
sequence <= 55
sequence < 54

A loop can run zero times. The condition is checked before the first round. In Exercise 3, an upper limit of 1 prints nothing, because 1 < 1 is false from the start. In Exercise 2, a start of 0 prints only Now!. With while True, the loop always runs at least once, but with a condition, it may not run at all.

A missing change gives a loop that never ends. If number -= 1 is removed from the countdown, the program prints 5 forever. In that case, press Ctrl + C in the terminal to stop it.

Key takeaways

  • while condition: repeats its block for as long as the condition is true, and stops by itself when it becomes false, with no break.
  • A loop like this needs a starting value, a condition and a change.
  • The condition is checked before every round, so the loop may not run at all.
  • <= includes the limit, and < leaves it out.
  • number -= 1 counts down and number += 1 counts up. number += 2 counts in steps of two.
  • = stores a value, while ==, > and < compare. Using = in a condition is a syntax error.
  • If the change inside the loop is missing, the loop never ends.

✦ 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: These exercises are 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 · loops · while-loop · counter · debugging