Junior Codes
Clockwork Woods

Adding Up in a Loop: Running Totals in Python for Kids

Start at zero and add as you go to count or total anything.

Start Wonderwild free with Pip

Try 5 starter activities free. Full access is ₹499 a year.

This mission is part of full access. The first missions are free to try.

Running totals, explained simply

How would you add up the apples in five baskets? You start at zero in your head, then add each basket one by one. Programmers call this a running total, or an accumulator.

In Python you create a variable set to 0 before the loop. Inside the loop you add to it each time round. After the loop, the variable holds the total.

The same pattern counts things too: add 1 instead of adding a value. It is one of the most useful patterns in all of programming.

Example: Count the harvest

baskets = [4, 7, 2, 5]
total = 0
for apples in baskets:
    total = total + apples
print(f"We picked {total} apples.")

Output

We picked 18 apples.

Try this

  • Move total = 0 inside the loop. Why does the answer go wrong?
  • Count how many baskets have more than 4 apples.
  • Use total += apples, a shorter way to write the same line.

Practise it in Wonderwild

“Count the harvest”

Mission 13 in Clockwork Woods, one of 36 Python missions in Wonderwild.

Your child writes and runs real Python in the browser while Pip reacts to every line. Nothing to install. For ages 7+.

Start Wonderwild free with Pip

Try 5 starter activities free. Full access is ₹499 a year.

This mission is part of full access. The first missions are free to try.

Questions parents and kids ask

Why must the total start before the loop?+

If you set it to 0 inside the loop, it resets every time round and forgets what it added before.

Is there a built-in way to add up a list?+

Yes, sum(baskets) does it in one step. Writing the loop yourself first helps you understand what sum() is doing, and the loop can do things sum() cannot, like skipping some values.

Prefer learning with a teacher?

Our live Python for Young Developers course teaches these ideas in small online classes led by a software engineer, with projects and feedback. Every live course also includes 12 months of full Wonderwild access for practice.

See the Python for Young Developers course