# Repeating with while

A while loop runs its indented block again and again as long as its condition stays True, so a program can count, keep going until a job is done, or keep asking until an answer makes sense.

Novice · Python · Updated November 19, 2026

By Gregor Dzierzon

## After this lesson you can

- Repeat a block with a while loop and a counter that starts, is checked and changes
- Find why a while loop never ends, stop it with Ctrl+C, and keep asking until an answer is valid

## Key takeaways

- A while loop checks its condition, runs its block when the condition is True, then checks again, and stops the first time the condition is False.
- A counting loop needs three parts, a starting value, a condition and a change inside the loop, such as wave = wave + 1, which += shortens to wave += 1.
- A loop whose condition never becomes False runs forever. Press Ctrl+C in the terminal to stop it, then add the change the loop is missing.
- A while loop can keep asking with input() until the answer is one the program can use.

A game keeps playing rounds until you quit. An app tries an upload again until it works. A sign-up form asks for your email until it looks like an email. Each of those is a loop: the same steps, run again and again, until something changes. This lesson shows Python's first loop, `while`.

The example is still Splash Valley Water Park: the wave pool, the line for the lazy river's tubes, and the height kiosk at the Big Splash slide.

## The same line, five times

The wave pool makes its waves in sets of five, and the speaker announces each one. Here's a first try:

```python
print("Wave 1")
print("Wave 2")
print("Wave 3")
print("Wave 4")
print("Wave 5")
```

```text
Wave 1
Wave 2
Wave 3
Wave 4
Wave 5
```

It works, but when the park switches to sets of 20, you copy fifteen more lines and hope you numbered them right.

Think of a recipe that says "keep stirring while the sauce is lumpy". You don't count the stirs in advance; before each one, you check the sauce. A **while loop** works the same way: before each round, it checks a condition, and it runs its indented block again for as long as the condition is `True`.

```python
wave = 1
while wave <= 5:
    print("Wave", wave)
    wave = wave + 1
print("That's the set. Next waves in 15 minutes.")
```

```text
Wave 1
Wave 2
Wave 3
Wave 4
Wave 5
That's the set. Next waves in 15 minutes.
```

The `while` line looks like an `if` line: the word, a condition, a colon, and an indented block. The difference is what happens after the block runs: an `if` moves on, while a `while` goes back up and checks its condition again. When `wave` reaches 6, `wave <= 5` is `False`, the loop ends, and the line after it runs. For sets of 20, you change one number.

## Three parts of a counting loop

A loop that counts needs three things, and the wave loop has all of them:

1. **A start:** `wave = 1`, before the loop.
2. **A condition:** `wave <= 5`, checked before every round.
3. **A change:** `wave = wave + 1`, inside the block, so the condition can become `False` one day.

Python has a shortcut for adding to a variable: `wave += 1`, read "wave plus-equals 1", means the same as `wave = wave + 1`. `-=` takes away the same way. This shortcut is called **augmented assignment**.

A `while` loop shines when you don't know the number of rounds in advance. The lazy river's tubes leave in groups of 6, and 23 guests are waiting:

```python
waiting = 23
rounds = 0
while waiting > 0:
    waiting -= 6
    rounds += 1
print("Rounds to clear the line:", rounds)
```

```text
Rounds to clear the line: 4
```

After three rounds, 5 guests are still waiting, so a fourth round runs; the loop worked that out by checking before each round.

## The loop that never ends

Leave out the change, and the condition never becomes `False`:

```python
wave = 1
while wave <= 5:
    print("Wave", wave)
```

`wave` stays 1, so `wave <= 5` stays `True`, and the loop prints `Wave 1` thousands of times a second, forever. This is an **infinite loop**, or endless loop; everyone writes one sooner or later. Stop it by clicking in the terminal and pressing **Ctrl+C** (on a Mac too: Control, not Command). Here's the end of a run on Windows, from a file named `waves.py` (your path will be your own):

```text
Wave 1
Wave 1
^CWave 1
Traceback (most recent call last):
  File "C:\Users\Sam\python-course\waves.py", line 3, in <module>
    print("Wave", wave)
    ~~~~~^^^^^^^^^^^^^^
KeyboardInterrupt
```

`KeyboardInterrupt` means "you stopped me from the keyboard" (the `^C` is your Ctrl+C, which some terminals show), and the traceback names the line Python was on. Now fix the loop: is its change missing, outside the loop's block, or moving the wrong way, such as `wave -= 1`?

## Asking until the answer makes sense

The height kiosk at the Big Splash slide asks each guest for their height. Someone who types `480` meant 4 feet 8 inches, not 480 inches. A `while` loop can keep asking until the answer is believable, using `or` from *Combining Conditions with and, or and not*:

```python
height = int(input("Your height in inches: "))
while height < 30 or height > 84:
    print("That doesn't look right. Please measure again, in inches.")
    height = int(input("Your height in inches: "))
print("Thanks! Your height:", height)
```

Here's a run where the guest types `480` first, then `56`:

```text
Your height in inches: 480
That doesn't look right. Please measure again, in inches.
Your height in inches: 56
Thanks! Your height: 56
```

The loop asks again only while the answer is out of range, so a guest who types `56` the first time never sees the warning. The question appears twice: once before the loop for the first answer, and once inside it for every try after. Typing a word such as `tall` still stops the program with a `ValueError`; *Handling Errors with try and except*, in Module 3, shows how to catch that too.

## Do it: fill the giant bucket until it tips

The giant bucket over Splash Valley's play area fills with the same amount of water every minute, and tips onto the guests below once it's full.

- `per_minute`: how many gallons of water pour in each minute.
- `capacity`: how many gallons the bucket holds before it tips.

The practice box can't wait for someone to type, so the starter gives you a function line, `def show_bucket(per_minute, capacity):`, and the tests call it with several buckets. Functions come in Module 2 (*What a Function Is: def and Calling*); for now, write your lines indented under that line, and use `per_minute` and `capacity` as variables that already hold one bucket's values.

**Your job:** inside `show_bucket(per_minute, capacity)`, use a `while` loop to print one line for each minute of filling, then `Splash!`:

- The bucket starts empty, at 0 gallons, before minute 1.
- Each minute, `per_minute` gallons pour in. Print `Minute 1: 50 gallons` (the minute's number, then the gallons in the bucket so far).
- Keep filling while the bucket holds less than `capacity`. Once it holds `capacity` or more, stop and print `Splash!`.

Worked through: with `show_bucket(50, 200)`, the bucket holds 50, 100, 150, then 200 gallons. 200 isn't less than 200, so the filling stops there, and it prints these five lines:

```text
Minute 1: 50 gallons
Minute 2: 100 gallons
Minute 3: 150 gallons
Minute 4: 200 gallons
Splash!
```

Practiced live on the lesson page: write the Python right there and check it.

## Check yourself

2 questions to try on the lesson page.

## So what

A `while` loop repeats work until something changes, which is how programs wait for good input, count down and keep going until a job is done, as long as something in the loop moves it toward its end.

## Key terms

- [while Loop](https://www.ask-codey.com/glossary/while-loop)
- [Infinite Loop](https://www.ask-codey.com/glossary/infinite-loop)
- [Augmented Assignment](https://www.ask-codey.com/glossary/augmented-assignment)

## Related

- [Counting with for and range](https://www.ask-codey.com/lessons/python-for-loops-and-range)
- [Combining Conditions with and, or and not](https://www.ask-codey.com/lessons/python-and-or-not)

Source: https://www.ask-codey.com/lessons/python-while-loops
