← Back to course
Code Crew G6+ / Week 04 / Wednesday
3/6
Week 04 Β· Loops

Wednesday

Repeat while
// for, while, break and a countdown clock
⏱ about 30 min

Wednesday: Repeat while

A new question arrives from the supply team: how many minutes can the engine run before the tank is empty? Rocket starts typing range, then stops.

"Wait," he says. "I do not know how many times to loop. That is the whole question."

Raven writes the rule in her notebook: keep burning while there is fuel. "What do you notice about that sentence?" she asks. "It does not give a number. It gives a condition."

Nova blinks a slow yellow. "Interesting," she says. "Python has a loop that checks a condition before every round, just like your sentence. Be sure the condition can become False."

Rocket writes the word while and thinks carefully about the last part of her hint.

The while loop

A while loop checks its condition, runs its block if the condition is True, and then checks again. It stops the first time the condition is False.

Use while when you do not know in advance how many rounds you need. Use for when you do.

# burn_time.py
fuel = 100
minutes = 0
while fuel > 0:
    fuel = fuel - 30
    minutes = minutes + 1
print(f"Fuel ran out after {minutes} minutes.")
print("Fuel left:", fuel)
Roundfuel > 0?fuel afterminutes after
1100 > 0 is True701
270 > 0 is True402
340 > 0 is True103
410 > 0 is True-204
Check-20 > 0 is Falseloop ends
Fuel ran out after 4 minutes.
Fuel left: -20
Change the burn to 25 units a minute. How many minutes does it run now?
WHY THIS EXERCISEAfter four rounds fuel is 0, and 0 > 0 is False, so the loop stops after 4 minutes.

Infinite loops

If nothing inside the loop changes the condition, it stays True forever. That is an infinite loop. The shell keeps running and never shows a new prompt.

Do not worry if it happens. Click in the shell and press Ctrl+C to stop it. You will see KeyboardInterrupt, and then you can fix the loop.

# This loop never ends: fuel never changes.
fuel = 100
while fuel > 0:
    print("Burning...")
SPOT THE PROBLEM
  • Read the question.
  • Tap your answer.
Why does the loop above never stop?
How do you stop a runaway loop in the IDLE shell?
At your computer
1. Type burn_time.py, save it and run it.
2. Change the burn to 25 and run it. Check your prediction.
3. Now make an infinite loop on purpose: delete the line fuel = fuel - 30 and run it.
4. Click in the shell and press Ctrl+C to stop it.
5. Put the line back, save, and run it once more.

Practice: how many doublings?

while is the right loop when the question is how many steps something takes. Here a signal doubles in strength until it reaches at least 1000.

# doubling.py
signal = 1
steps = 0
while signal < 1000:
    signal = signal * 2
    steps = steps + 1
print("Steps:", steps)
print("Signal:", signal)
Steps: 10
Signal: 1024
TRACE DOUBLING.PY
  • Read the question.
  • Tap your answer.
Why does the loop stop when signal is 1024?
What is signal after 3 steps?
Change 1000 to 100. How many steps now?
Start signal at 5 with the limit 1000. How many steps?
In that run, what is the final signal?
StatementTrue or false?
A while loop checks its condition before each round.?
If the condition is False at the very start, the block runs once anyway.?
Use while when you do not know how many rounds you need.?
Ctrl+C deletes your program file.?
WHY THIS EXERCISEA False condition at the start means zero rounds, and Ctrl+C only stops the running program.

You can now repeat until something happens, and you know how to stop a loop that runs away. Tomorrow you will leave a loop early with break.

← Tuesday