Counters and totals in a Python loop

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Counters and totals in a Python loop

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which line should go BEFORE the loop to start a counter?
A count = 0
B count += 1
2. Which line adds 1 to count each time the loop repeats?
A count = 1
B count += 1
3. Where should total = 0 be placed to build a running total?
A Before the loop starts
B Inside the loop, at the top
4. Which line adds the number stored in score to a running total?
A total += score
B total = score
5. What is count after this code runs? count = 0 for i in range(4): count += 1
A 1
B 4

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1count += 1 Acount = 0 for i in range(5): count += 1
2total += score B=+ is written instead of +=, so total is just set to n each time instead of adding to it. It should be total += n.
3Set a counter to 0 before the loop so it starts with nothing counted CAdds the value of score onto total, building a running total inside the loop
4Add each number from a list to a running total inside a for loop Dtotal = 0 for n in marks: total += n
5for i in range(5): count = 0 count += 1 Ecount 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.
6total = 0 for n in marks: total =+ n FAdds 1 to the variable count each time it runs, so it counts how many times the loop repeats

Definitions

count += 1

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
count = _ for i in range(5): count += 1
2.
total = 0 for n in range(1, 4): total __ n
3.
score = 0 for n in [4, 7, 9]: score += _
4.
count = 0 for name in ["Mia", "Leo"]: count += _
5.
_____ = 0 for p in [5, 10, 15]: total += p

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A count = 0
B for i in range(5):
C count += 1
D print(Count)
Question 2
A total = 1
B for n in range(1, 4):
C total += n
D print(total)
Question 3
A count = 0
B while count < 3
C count += 1
D print(count)
Question 4
A total = 0
B for n in [4, 6, 10]:
C total += 1
D print(total)
Question 5
A score = 0
B for i in range(3):
C score += 10
D print('Score:' score)

Answer Key

Counters and totals in a Python loop
Section A: Quick Questions
Q1
A — count = 0
Q2
B — count += 1
Q3
A — Before the loop starts
Q4
A — total += score
Q5
B — 4
Section B: Match the Pairs
Q1
count += 1 → Adds 1 to the variable count each time it runs, so it counts how many times the loop repeats
Q2
total += score → Adds the value of score onto total, building a running total inside the loop
Q3
Set a counter to 0 before the loop so it starts with nothing counted → count = 0 for i in range(5): count += 1
Q4
Add each number from a list to a running total inside a for loop → total = 0 for n in marks: total += n
Q5
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.
Q6
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.
Section C: Definitions
Q1
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.
Section D: Fill in the Blanks
Q1
count = 0 for i in range(5): count += 1
Q2
total = 0 for n in range(1, 4): total += n
Q3
score = 0 for n in [4, 7, 9]: score += n
Q4
count = 0 for name in ["Mia", "Leo"]: count += 1
Q5
total = 0 for p in [5, 10, 15]: total += p
Section E: Spot the Error
Q1
D — Python cares about capital letters. The variable is called count, not Count, so Count causes a NameError. It should be print(count).
Q2
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.
Q3
B — A while line must end with a colon. It should be while count < 3:
Q4
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.
Q5
D — The items inside print() need a comma between them. It should be print('Score:', score).