Loops · Lesson 12 of 27
while loop
A while loop repeats as long as its condition is true.
i = 1
while i <= 5:
print(i)
i = i + 1 # without this line the loop never ends!
print("Done")This prints 1 to 5, then Done.
i += 1 is a short way to write i = i + 1. The same works for -=, *= and so on.
If the condition never becomes false, you get an infinite loop. Here, the code is stopped after 5 seconds, so don't worry about breaking anything.
Your task
Read a number n and count down from n to 1 (one number per line), then print Liftoff!.
For n = 3:
3 2 1 Liftoff!