Lesson 4 of 12
Structured learning draftProcess a collection with a loop
A daily sales total should work for three transactions and for an empty day. Use a loop and an accumulator, then check that the starting value and repeated operation produce the result you expect.
Learning objectives
- Visit each list item once.
- Maintain a running total.
- Explain the empty-list result.
Process a collection with a loop: the working sequence
Trace the three stages of process a collection with a loop and compare them with the example.
The list provides three quantities in a defined order.
The loop adds each quantity to the existing total.
The accumulator finishes at 9; an empty list would leave it at zero.
A for loop visits each item in an iterable. The accumulator begins at zero and increases once per quantity. An empty collection runs the body zero times, leaving the total at its starting value.
quantities = [3, 2, 4]
total = 0
for quantity in quantities:
total += quantity
print(f'Total sold: {total}')Total sold: 9Try it, then change it
- Trace total after each iteration: 3, 5, then 9.
- Use an empty list and predict the displayed total.
- Move total = 0 inside the loop temporarily and explain the wrong result; then restore it.
Common mistakes
- Resetting the accumulator inside the loop.
- Assuming a loop body runs once for an empty list.
- Adding an item twice in separate branches.
Practice activity
Apply the lesson
Total the lengths of three fictional delivery routes. Include an empty route list and a single-route list in your checks. Keep the changed example, its output and a short explanation of whether the result matched your prediction.
How to check your work
- Write down the expected result before running or submitting your work.
- Try a normal example, an empty or missing input, and a boundary case where the rule changes.
- Compare the actual result with your prediction. Record the smallest example that explains a difference.
- Explain one change you made and why it improved the result. Save your evidence in the lesson notes.
When you get stuck · Python
The script stops with a traceback or gives an unexpected value.
- Find the cause
- Read the exception type and message on the last line, then find the referenced line in your own file. Compare the value and type of each input with what that operation expects.
- Try this
- Reduce the failing case to a few lines. Check variable spelling, numeric conversion and file paths. Handle only the exception you expect; do not hide an unknown error with a broad except block.
- Check the fix
- Run the corrected case and a deliberately invalid input. The valid input produces the expected result; the invalid input gives a clear message or raises the documented exception.
Use this course-wide example when it fits your task. For a different failure, record the input, expected result, actual result and smallest steps that reproduce it.
Watch, pause, practise
Free video lessons
Choose the chapter that matches your task. Videos open on the original provider’s site; playback uses additional data.
CS50: programming with Python
Choose the lecture on functions, loops, exceptions, tests or files that matches your current task.
Try it yourself: Before running an example, predict its output. Try an empty input and record how the program behaves.
Source checked 2026-10-07 · Free learning material; optional certificates may cost extra.Check your understanding
What is total when quantities is [] in this example?
Lesson summary
- Initialise an accumulator before the loop.
- One iteration should correspond to one intended item.
- An empty collection is an important test case.
Sources and further reading
- Built-in types: sequence operationsPython Software Foundation - accessed 2026-10-07
- Python language referencePython Software Foundation - accessed 2026-10-07
Personal study note
Your notes
Keep the input, expected result, actual result and what you changed. Save before leaving this lesson. Download a copy to keep it outside this browser.