Phase 1: Prepare
Step 1 Warm-up recall
From M3.L2: what stops a while loop?
Private: nothing you type here is sent or stored.
Step 2 Learning goal
By the end of this lesson you can:
- build a running total (Foundation);
- count items that match a condition (Core);
- compute an average after the loop (Core).
Phase 2: Teach
Step 3 A familiar example
A cashier adds each price to a running total on the till, and counts the items. At the end, the total and the count are the answer. The loop does the same.
Step 4 Clear explanation
A loop becomes useful when it accumulates a result. Start a variable before the loop, change it inside, and use it after.
total = 0
for i in range(1, 5):
total = total + i
print(total)10To count items that match a test, put an if inside the loop:
count = 0
for n in range(1, 11):
if n % 2 == 0:
count = count + 1
print(count)5To find an average, divide the total by the count after the loop ends: 10 / 4 gives 2.5. The starting value matters. A total starts at 0 so that the first addition is right. Put the print line outside the loop to show only the final answer.
Step 5 Worked example (4 marks)
What does this print? s = 0, for k in range(1, 6): s = s + k, print(s)
- k takes 1, 2, 3, 4, 5. (1 mark)
- s becomes 1, 3, 6, 10, 15. (1 mark)
- The print is after the loop, so it shows the last s. (1 mark)
- Answer: 15. (1 mark)
Go deeper: the professor's notes
total = 0.
Each pass.
After the loop.
Pattern
| Variable | Starts at |
|---|---|
| total | 0 |
| count | 0 |
At work: Orders against a target
212 orders to fill.
- Count the orders filled.
- Stop when the count reaches 212.
What a careful practitioner decides. Make the stop condition depend on a counter that is really updated.
Phase 3: Practise
Step 6 Learner practice (instant-marked)
1. A running total should start at:
Zero adds nothing.
2. The total of range(1, 4) values (1, 2, 3) is:
1 + 2 + 3.
3. To count matching items, you use a loop with:
Add 1 when the test passes.
4. The average of a total of 10 over a count of 4 is:
10 / 4.
Phase 4: Close
Step 7 Recap
- Start the total at 0 before the loop.
- Add inside the loop; print after it.
- Count with an if and a counter; average after the loop.
Step 8 Exit ticket (ungraded)
Write a loop that adds the numbers 1 to 10 and prints the total.
Private: nothing you type here is sent or stored.
Step 9 Go further (Extension)
Write a program that counts how many numbers from 1 to 50 are multiples of 7.
In industry: Knight Capital: orders sent without end
On 1 August 2012 an old function on one server was triggered by a repurposed flag. The code that recorded completed orders had been changed, so the function never saw an order as filled and kept sending. In 45 minutes it sent more than 4 million orders to fill 212 customer orders.
Link to this lesson. A counter that never reaches its target is an infinite loop. Always test that the stop condition can be met.
Source: SEC press release 2013-222; Wikipedia, Knight Capital Group
Common mistakes
| Mistake | Why it fails |
|---|---|
| Setting total = 0 inside the loop | It resets on every pass. |
| Printing inside the loop | You see every step, not the final answer. |
| Dividing by the wrong count | Count the items, not the last value. |
Limits. Sums of many decimals can show tiny rounding differences in Python.