ShareYourGroupShareYourGroupPython

Lesson 9 of 21 · about 7 minutes with the exercises

While loops

A loop repeats a block of code. The while loop repeats as long as a condition stays true. Use it when you do not know in advance how many rounds you need: keep asking until the answer is valid, keep halving until the number is small, keep playing until the player quits.

The shape of a while loop

count = 1
while count <= 5:
    print("Round", count)
    count += 1
print("Finished")

Output

Round 1
Round 2
Round 3
Round 4
Round 5
Finished

Python checks the condition. If it is true, it runs the indented block, then goes back up and checks again. When the condition becomes false, the loop ends and the program carries on below it.

Every counting loop has three parts, and all three are needed:

  1. A starting value before the loop: count = 1.
  2. A condition that will become false one day: count <= 5.
  3. A line inside the loop that moves towards that day: count += 1.

The loop that never ends

Leave out the third part and the condition stays true for ever. The program prints the same line without stopping. This is an infinite loop.

count = 1
while count <= 5:
    print("Round", count)
    # forgot to add 1 to count

If this happens to you, stop the program by pressing Ctrl and C together, or use the Stop button of your editor. Then look for the line that should have changed the variable.

Keeping a running total

A loop often builds up an answer a little at a time. Start the total at zero before the loop.

total = 0
number = 1
while number <= 100:
    total += number
    number += 1
print("Sum of 1 to 100:", total)

Output

Sum of 1 to 100: 5050

Counting down

seconds = 3
while seconds > 0:
    print(seconds)
    seconds -= 1
print("Go!")

Output

3
2
1
Go!

Leaving early with break

break ends the loop at once, even if the condition is still true. A common pattern is while True: with a break inside. while True on its own would run for ever, so the break is the only way out.

savings = 0
month = 0
while True:
    month += 1
    savings += 1500
    if savings >= 10000:
        break
print("Target reached in month", month)

Output

Target reached in month 7

This same pattern is used to ask a question until the answer is acceptable. The person typed 0, then 150, then 42.

while True:
    age = int(input("Age (1 to 120)? "))
    if 1 <= age <= 120:
        break
    print("Please enter a sensible age.")
print("Thank you. Age recorded:", age)

Output

Age (1 to 120)? 0
Please enter a sensible age.
Age (1 to 120)? 150
Please enter a sensible age.
Age (1 to 120)? 42
Thank you. Age recorded: 42

Skipping one round with continue

continue jumps straight back to the condition check, skipping the rest of the block for this round only.

n = 0
while n < 10:
    n += 1
    if n % 3 == 0:
        continue
    print(n, end=" ")
print()

Output

1 2 4 5 7 8 10 

The multiples of 3 are missing from the output. Be careful to change the loop variable before the continue. If n += 1 came after it, the loop would get stuck on 3 for ever.

A loop where the count is unknown

How many times can you halve 1000 before it drops below 1? You do not know, so while is the right tool.

value = 1000
steps = 0
while value >= 1:
    value = value / 2
    steps += 1
print("Halved", steps, "times")

Output

Halved 10 times

While or for?

The next lesson covers the for loop. As a rule of thumb: use for to go through a list of things or to repeat a known number of times. Use while when the loop should run until something happens.

Try it yourself

Exercise 1. Print the table of 7 from 7 times 1 to 7 times 10 using a while loop.

Show one answer
i = 1
while i <= 10:
    print("7 x", i, "=", 7 * i)
    i += 1

Output

7 x 1 = 7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35
7 x 6 = 42
7 x 7 = 49
7 x 8 = 56
7 x 9 = 63
7 x 10 = 70

Exercise 2. Starting from 1, keep doubling a number until it is more than 1000. Print the final number and how many doublings it took.

Show one answer
number = 1
times = 0
while number <= 1000:
    number *= 2
    times += 1
print(number, "after", times, "doublings")

Output

1024 after 10 doublings

Exercise 3. Add up the digits of 9875 using a loop with % 10 and // 10.

Show one answer
n = 9875
total = 0
while n > 0:
    total += n % 10
    n = n // 10
print(total)

Output

29

Examples on this page were run with Python 3.14 on 12 November 2026. The output shown is the real output.

This is an independent learning site. It is not connected with or approved by the Python Software Foundation. "Python" is a trademark of the Python Software Foundation and is used here only to name the language.