12. Countdown
Your program reads one line holding a whole number n. Print the numbers from n down to 1, each on its own line,
then Liftoff! on the last line.
If n is 0 or negative, there is nothing to count down: print only Liftoff!.
Example 1
Input
3Output
3
2
1
Liftoff!ExplanationOne line each for 3, 2 and 1, then Liftoff!
Example 2
Input
0Output
Liftoff!ExplanationThere is nothing to count down from 0.
Constraints
nis a whole number from -100 to 100.- Print nothing else. A prompt inside
input()is fine: the tests leave it out of the output.
Submit also runs 4 more tests you can't see.
Practises While Loops (lesson 14).
- A
while n > 0:loop runs as long as there is something left to count. Whennstarts at 0 or below, it doesn't run at all. - Inside the loop,
print(n), then makenone smaller withn -= 1. Without that line the loop never ends. - Print
"Liftoff!"after the loop, not inside it, so it comes once, last.
Solve it without hints for 3 stars, with hints for 2, after seeing the solution for 1.
One way to solve it, once you've had a go.
Solving it after seeing the solution earns 1 star.
Was this challenge helpful?
Thanks!
Your Submits show here, with their code, so you can go back to one. They stay in this browser.
Code
# Read a whole number n. Print n, n - 1, ... down to 1, # one per line, then "Liftoff!". n = int(input())
Run your code to see how it does on the test cases, or Submit to check every test.