>_Code Club
← All exercises

python

Loops

Computers never get bored of repeating things. A for loop repeats code for each value:

for i in range(5):
    print("Lap", i)

range(5) gives 0, 1, 2, 3, 4 (5 numbers, starting at 0).

range can also take a start, an end (not included), and a step:

for i in range(10, 0, -2):
    print(i)    # 10, 8, 6, 4, 2

A while loop repeats as long as a condition is true:

fuel = 3
while fuel > 0:
    print("Vroom!", fuel)
    fuel = fuel - 1

Oops, wrote a loop that never ends? Press ■ Stop.

Your mission

Write countdown(start) that prints the numbers from start down to 1, one per line, then prints Liftoff!.

countdown(3) should print:

3
2
1
Liftoff!
Next exerciseLists →
Loading the editor…