← VCE Software Development U3O1

Counters and totals in a Python loop

Set count = 0 before a loop, then use count += 1 inside it to add 1 each repeat. Use total += value to build a running total.

Understanding Counters and totals in a Python loop in VCE Software Development U3O1

What it does: The line count = 0 sits before the loop and gives the counter a starting value. Inside the loop, count += 1 adds one each repeat. total += score adds score to what is already in total. total = score throws away the old total. total grows each repeat: 0 + 2 + 5 + 3 = 10. Using = instead of += would leave only 3. += is a shortcut. It takes the current total, adds value, and stores the answer back in total. The + must come before the =. Writing =+ replaces the total with the new value, so the running total is lost.

Common mistakes: Python cares about capital letters. The variable is called count, not Count, so Count causes a NameError. It should be print(count). A running total must start at 0 before the loop. Starting at 1 makes the answer 1 too big. It should be total = 0.

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 10–47) and word search (Modules 10–71).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
Which line should go BEFORE the loop to start a counter?
count = 0count += 1
Matches
for i in range(5): count = 0 count += 1
↓
count is set to 0 inside the loop, so it resets every repeat and always ends up as 1. It should be set to 0 before the loop.
Applying
Meanings
Term
count += 1
Meaning
Set count = 0 before a loop, then use count += 1 inside it to add 1 each repeat. Use total += value to build a running total.
Practice
total = 0
Testing
Gaps
count =
Check Answer
Spots
3 count += 1
4print(Count)

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Stag' badge — Forest Legend, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

1. Which line should go BEFORE the loop to start a counter?
A. count = 0 ✓
B. count += 1
A counter must start at 0 before the loop. If you set it to 0 inside the loop, it resets every repeat.
2. Which line adds 1 to count each time the loop repeats?
A. count = 1
B. count += 1 ✓
count += 1 adds 1 to the old value. count = 1 just sets it to 1 every time, so it never grows.
3. Where should total = 0 be placed to build a running total?
A. Before the loop starts ✓
B. Inside the loop, at the top
If total = 0 is inside the loop, the total is wiped back to 0 on every repeat. Set it once, before the loop.
4. Which line adds the number stored in score to a running total?
A. total += score ✓
B. total = score
total += score adds score to what is already in total. total = score throws away the old total.
5. What is count after this code runs? count = 0 for i in range(4): count += 1
A. 1
B. 4 ✓
The loop repeats 4 times and adds 1 each time: 0 + 1 + 1 + 1 + 1 = 4.

Match the Pairs

count += 1 → Adds 1 to the variable count each time it runs, so it counts how many times the loop repeats
total += score → Adds the value of score onto total, building a running total inside the loop
Set a counter to 0 before the loop so it starts with nothing counted → count = 0 for i in range(5): count += 1
Add each number from a list to a running total inside a for loop → total = 0 for n in marks: total += n
for i in range(5): count = 0 count += 1 → count is set to 0 inside the loop, so it resets every repeat and always ends up as 1. It should be set to 0 before the loop.
total = 0 for n in marks: total =+ n → =+ is written instead of +=, so total is just set to n each time instead of adding to it. It should be total += n.

Key Term

count += 1
Set count = 0 before a loop, then use count += 1 inside it to add 1 each repeat. Use total += value to build a running total.

Fill in the Blanks

1. count = 0 for i in range(5): count += 1
Hint: Type the starting value the counter has before the loop begins.
2. total = 0 for n in range(1, 4): total += n
Hint: Type the operator that adds a value to the variable and saves the result.
3. score = 0 for n in [4, 7, 9]: score += n
Hint: Type the loop variable that is added to the running total each repeat.
4. count = 0 for name in ["Mia", "Leo"]: count += 1
Hint: Type the amount a counter goes up by for each item it counts.
5. total = 0 for p in [5, 10, 15]: total += p
Hint: Type the variable name that holds the running total before the loop.

Spot the Error

Question 1
A count = 0
B for i in range(5):
C count += 1
D print(Count)
Line D — Python cares about capital letters. The variable is called count, not Count, so Count causes a NameError. It should be print(count).
Question 2
A total = 1
B for n in range(1, 4):
C total += n
D print(total)
Line A — A running total must start at 0 before the loop. Starting at 1 makes the answer 1 too big. It should be total = 0.
Question 3
A count = 0
B while count < 3
C count += 1
D print(count)
Line B — A while line must end with a colon. It should be while count < 3:
Question 4
A total = 0
B for n in [4, 6, 10]:
C total += 1
D print(total)
Line C — This adds 1 each time, so it counts the repeats instead of adding up the numbers. To build a running total, use total += n.
Question 5
A score = 0
B for i in range(3):
C score += 10
D print('Score:' score)
Line D — The items inside print() need a comma between them. It should be print('Score:', score).

Student with a class code? Join your class →