Day 13 · ~13m

Python while Loops: Run Until a Condition Changes

Learn when to use while loops instead of for loops. A while loop keeps running until a condition changes — perfect for unknown-count iteration.

student (curious)

Day 7 we used a for loop to go through a list of sales. But what if I don't know how many items I'll process before I hit my sales target?

teacher (neutral)

Excellent question. That's the exact moment while loops shine. A for loop knows how many times it'll run before it starts — it has a list. A while loop doesn't. It just keeps going until something changes. That's a very different kind of power.

Remember on Day 12 when we used is_valid_sale() to check if a sale passes our business rules? The function returned True or False. Now we're going to use that same boolean logic, but inside a loop condition:

while is_valid_sale(name, amount):
    # Process this sale
    total += amount
    # Move to next sale

The loop checks the condition before each iteration. If it's True, the loop body runs. If it becomes False, the loop stops. No counter, no fixed list — just "keep going until the condition flips."

student (thinking)

So what's the difference between total < target and total >= target?

teacher (neutral)

Good catch. If you write while total < target:, you keep looping as long as the total is still below target. The moment it reaches or exceeds target, total < target becomes False and the loop exits. That's when you've hit your goal and you're done.

target = 10000
total = 0
index = 0

while total < target and index < len(sales):
    name, amount = sales[index]
    total += amount
    index += 1

print(total)  # The final accumulated total

Notice we also have index < len(sales). That's a safety guard — if we run out of sales before hitting the target, the loop stops there too. Both conditions must be true to keep looping.

student (confused)

Wait... what happens if you forget to update index? Or forget index += 1?

teacher (neutral)

Then you get an infinite loop. The loop never moves forward, the condition never changes, and the program runs forever. That's one of the most common while loop mistakes.

I have a story: last week I was testing sales processing, and I forgot the index += 1 line. Eleven minutes later, the terminal was still running. I thought the server had crashed. Turned out I'd created a loop that would keep adding the same sale over and over for all eternity. Once I added that one line, the loop stopped in a fraction of a second. That's why you update the loop variable.

student (excited)

Oh! So it's like... the loop variable has to move forward. Every iteration, something has to change, or the condition will never become false.

teacher (neutral)

Exactly. Every while loop needs:

  1. A condition that can become False
  2. Something inside the loop that changes the condition

Without #2, the condition never flips and you're stuck forever.

student (focused)

What comes next? Like, once I can process sales with a while loop and accumulate a total, what's the next step?

teacher (neutral)

Great question. Tomorrow we're diving into lists — not just iterating through them, but building them. You'll learn how to take those valid sales you're processing and actually collect them into a new list as you go. Right now you're computing a total. Soon you'll be collecting the actual sale records, filtering out the invalid ones, building up a dataset. That's where it gets powerful.