×

python · level 1 · unit 5 · lesson 2

  1. meet it
  2. guess first
  3. your turn
  4. fix it
  5. nailed it

Tap a green word if you get lost.

meet it

A loop keeps going while something is True.

Think of knocking on a door. You keep knocking while nobody answers. You do not know how many knocks it will take. A while loop works the same way: it repeats as long as its condition is True.

Most while loops use a variable that counts, like count below. Create it before the loop. Check it in the condition. Change it inside the loop, so the condition turns False one day and the loop ends.

Forget to change it and the condition stays True forever. That is an : the program goes round and round and never moves on. It is a common slip, and the fix is always the same: change the variable inside the loop.

count = 1
while count <= 3:
    print("Knock knock", count)
    count = count + 1
print("Who's there?")

Delete the last indented line and count stays 1 forever, so the knocking would never stop.