Countdown Timer Using a While Loop

Easy ⏱ 8 min 76% acceptance ★★★★★ 4.6
Write a function countdown(n) that returns a list counting down from n to 1, followed by the string "Liftoff!" as the final element, built using a while loop.

Examples

Example 1
Input
countdown(3)
Output
[3, 2, 1, "Liftoff!"]
Explanation

Counts down then appends the liftoff message.

Example 2
Input
countdown(1)
Output
[1, "Liftoff!"]
Explanation

Immediately reaches 1 then liftoff.

Constraints

  • n is a positive integer.

Topics

Loopswhile loop

Companies

SwiggyZomato

Hints

Hint 1

Use a `while n > 0:` loop, appending n then decrementing.

Hint 2

Append "Liftoff!" once the loop finishes.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.