Command Palette

Search for a command to run...

Hectal
Phase 9: Iterators, Generators and Decorators
Python · Exercise 1 of 6Next
Beginner

Write a countdown generator

Write a generator function countdown(n) that yields n, then n - 1, and so on down to 1. countdown(0) yields nothing.

Then use a for loop to print the values of countdown(3), one per line.

What Check tests

  1. countdown(3) is a generator
  2. list(countdown(3)) is [3, 2, 1]
  3. countdown(0) yields nothing
  4. A negative start yields nothing
  5. It ends with StopIteration after 1
  6. It is lazy: a huge countdown starts at once
  7. The program prints 3, 2, 1

Stuck? One hint at a time

The solution unlocks after your first Check. Try first: that is where the learning happens.

main.py
Sign in to run your code.

Output

Press Run to see what your program prints.