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
countdown(3)is a generatorlist(countdown(3))is[3, 2, 1]countdown(0)yields nothing- A negative start yields nothing
- It ends with
StopIterationafter 1 - It is lazy: a huge countdown starts at once
- 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.