Lesson 13 of 34

while Loops: Repeat While True

8 min 25 XP

Use while when you don't know in advance how many times to repeat. The loop keeps going while its condition is True.

Example: edit me and press Run

Output Loading Python (first time takes a few seconds)
Press Run to see the result here.

If you forget count = count + 1, the loop never ends (an infinite loop). If that happens, press Stop.

Example: if ₹1000 of savings grows 10% a month, how many months until it reaches ₹2000?

Example: edit me and press Run

Output Loading Python (first time takes a few seconds)
Press Run to see the result here.

months += 1 is short for months = months + 1.

Challenge

+25 XP

Use a while loop to count down from 5 to 1, printing each number on its own line. Then print Go!

Output Loading Python (first time takes a few seconds)
Press Run to see the result here.