──────────────────────────────────────

14. Game Loops · Keep a Running Score

Use an accumulator to combine one new value per round.

Lesson files · lessons/loops-and-games/totals-and-counters

Before you start
- Use for and range() to repeat an action.
- Read a whole number with int(input()).

By the end
- Use a variable to collect a total across repeated input.
- Trace how the total changes after each reading, including zero.

Where you can use it
Add up the points from several basketball rounds. Read one score per round and print the total after the last one.

Use range(3) to get the values 0, 1 and 2 and make the loop run three times. In this example the loop number counts the rounds. The program reads a new score during each round.

Set total = 0 before the loop, then add each score to that same total. The update total += points means total = total + points. Trace the example's total after it reads 5, then 0, then 7.

Predict
What happens after the second input, which is 0?
- The total resets to 0.
- The total stays at 5.

Worked example (example.py)

total = 0
for round_number in range(3):
    total += int(input())
print(f"Total: {total}")


Example input
5
0
7

Example result
Total: 12

Read four scores and print their sum as Score: followed by the number.

Starter program (starter.py)

total = 0
for round_number in range(3):
    total += int(input())
print(f"Total: {total}")


Starter input
5
0
7

Hint
Put total = 0 above the loop, and change the loop to repeat four times.

Case 1. Try the sample
Input
1
2
3
4
Expected output
Score: 10

Case 2. Zero points
Input
0
0
0
0
Expected output
Score: 0

Stretch
Read a round count first so the program can handle a tournament of any length.

Reflect
What would happen if you indented total = 0 inside the loop?

Check your prediction
The total stays at 5. Adding zero keeps the previous total.
