Skip to content
Tactiverge

School/△ Builder/Week 6

while loops: repeat until it's done

A motor cools by 20 % of its excess heat each minute. How long until it is safe to touch, without knowing the answer in advance?

About 35 min · 3 goals · Checked by automated tests

1.Why should I care?

Sometimes you don't know how many steps a job takes: cool until safe, fill until full, retry until a reply arrives. A while loop keeps going until a condition stops being true, which is exactly how many simulations and controllers work.

  • ◇ Use a while loop when the number of passes is not known in advance.
  • ◇ Make sure something inside the loop moves it towards its stopping condition.
  • ◇ Choose between for and while for a given engineering task.

2.Predict

excess = 60.0, and while excess > 5: excess = excess * 0.8. Roughly how many passes?

Guess first. Being wrong here is how the surprise works.

3.Discover

In the Code Lab, type the cooling program from this lesson and run it: do you get 12 minutes? Then change 0.8 to 0.9 (slower cooling) and predict before running. Finally, break it on purpose by deleting the multiply line, and add a safety limit so it still stops.

Can't use the interactive? Read a description instead

In the Code Lab the cooling program prints '12 4.12': the excess temperature falls 60.0, 48.0, 38.4, 30.72, 24.58, 19.66, 15.73, 12.58, 10.07, 8.05, 6.44, 5.15, 4.12 °C, so the loop stops after 12 passes. With 0.9 instead of 0.8 it takes 24 passes.

4.Checkpoint: guided practice

Check what you just discovered before going on. Hints appear after a wrong answer.

Practice 1 · a fresh version every time

A motor is 75 °C hotter than the room. Each minute of cooling multiplies the difference by 0.5. The program runs: while excess > 5: excess = excess * 0.5; minutes = minutes + 1 (starting from excess = 75, minutes = 0). What is minutes when the loop ends?

min

Practice 2

Which while loop never ends?

5.Understand

The cooling simulation

excess_c = 60.0      # motor is 60 °C above the room
minutes = 0
while excess_c > 5:
    excess_c = excess_c * 0.8
    minutes += 1
print(minutes, round(excess_c, 2))   # 12 4.12

The condition is checked before every pass. If it is false at the start, the body never runs.

Every while loop needs progress

Something inside the loop must move it towards stopping: here excess_c shrinks. If nothing changes, the loop runs for ever. For machines, add a guard as well: while excess_c > 5 and minutes < 120: stops even if the model or a sensor misbehaves.

for or while?

Use for when you know what to go through: every reading in a list, each of 10 test cases. Use while when you stop on a condition: until the tank is full, until the error is small, until the user types 'q'. A for loop over a list always ends; a while loop ends only if you make it.

Explain it another way

Stirring tea until the sugar has dissolved: you don't count stirs in advance, you check each time and stop when it's done.

Worked example 1

A tank holds 1000 L and drains 15 % of what is left each minute. With a while loop, how many minutes until it is below 500 L?

1000 → 850 → 722.5 → 614.1 → 522.0 → 443.7. After 5 minutes it is below 500 L, so the loop runs 5 times: while level_l >= 500: level_l = level_l * 0.85; minutes += 1.

Common mix-ups

  • A while loop always runs at least once.
    ✓ It checks first. If the condition is false at the start, the body never runs.
  • Infinite loops only happen when you write while True.
    ✓ Any condition that can never become false, like != on a value that skips past the target, loops for ever.

6.Practice on your own

Practice, not a test. Wrong answers get a hint and another try.

Practice 3

Which job suits a while loop best?

Practice 4

x = 2. How many times does while x > 5: x = x - 1 run?

7.Apply it

Simulate a phone charging: start at 12 % and add 7 % each 10-minute step while below 80 %. Print each step and the total time. Then add a guard so the loop also stops if the battery temperature (add 0.4 °C per step from 25 °C) exceeds 35 °C.

8.Reflect

Concepts: Decisions and loops · Simulation and time stepping