while loop

Repeat code as long as a condition is true. Use it when you do not know the number of turns.

Syntax

while condition:
...
  • break leaves the loop, and continue skips to the next turn.

Example

n = 3
while n > 0:
    print(n)
    n -= 1

Output:

3
2
1

Common mistakes

  • If nothing inside the loop changes the condition, it never ends.

Practise it