Iterators and generators · 7 / 15 Exercise Easy

Countdown with a generator

Objective

Create a Python program with your first generator function, which uses yield instead of return.

Instructions

  • Define def countdown(n), which, while n is greater than 0, yields (yield) the value of n and reduces it by 1.
  • Read an integer n (at least 1).
  • Show on one line the values produced by the generator, separated by spaces (print(*countdown(n))) and, on the next line, Liftoff!.
Input
5
Output
5 4 3 2 1
Liftoff!

Open the editor in full screen: the statement on the left and your code, the output and the tests on the right.

Start exercise