Quarry School

Keep a running total

Subject: Computing
Level 1 · FoundationsAbout 6 min read3 new terms
How this was estimated

Programmatic estimate. Rung levels, worked steps, prerequisites, terms and sentence length contribute to this level. It describes the lesson, not your ability.

Reading counts the whole lesson, including folded explanations, at 180 words a minute. Practice allows two minutes per item. These are planning assumptions; take the time you need.

AI estimate unavailable.

Path to here

Open the unit’s lesson list to follow the lesson order.

    Quick review (2 minutes)

    In plain words

    Picture a cashier starting a sale at 0.

    Rule

    A running total or a count is set to its starting value before the loop and changed inside the body, so each pass builds on the last. The biggest value so far starts with the first value and is replaced inside the loop when an if finds a bigger one. x += 1 adds 1 to x.

    Step

    1. Decide what to keep: a total, a count or the biggest value.

    2. Before the loop, give it a starting value: 0 for a total or a count, the first value for the biggest. A line such as total = total + steps reads the name before it changes it, so the name needs a value first.

    3. Inside the body, change it on the passes that need it: add each value to a total, add 1 when a pass matches, or replace the biggest when an if finds a bigger value.

    4. After the loop, use the result: display it, or, when the count is above 0, divide the total by the count for an average.

    5. Trace two passes by hand and check that the value after each pass builds on the one before.

    Trap

    A total set to 0 at the top of the body starts again on every pass. With total = 0 as the body's first line, the step program would display Total: 4000, the last day's steps alone, and Average: 1000.0, that one day divided by 4. Put the starting value above the for line.

    Starting the biggest value at 0 goes wrong when every value is below 0: no value is bigger than 0, so the program would display 0, a number that was never read. Start with the first value instead, as the worked example does.

    1 In plain words

    Picture a cashier starting a sale at 0. Each item adds its price, and the screen shows the total so far.

    A loop can keep a running total in the same way. A name is set to 0 before the loop, and each pass adds one value to it. After the loop, the name holds the sum of every value.

    The same plan keeps a count: start at 0 and add 1 on each pass that matches. It also keeps the biggest value so far: start with the first value, and replace it on any pass that finds a bigger one.

    Remember one sentence: set the total before the loop and add to it inside. To look up the short form count += 1, see section 7.2.1 of the Python 3.13 language reference, on augmented assignment.

    2 Picture it

    Passsteps readtotal so far140004000265001050035500160004400020000

    A table of the four passes of the step-count program. The total starts at 0. After the first pass it is 4000, after the second 10500, after the third 16000 and after the fourth 20000. The values are the input of the first worked example. The last row is marked.

    The running total of four days of steps

    3 Why

    The line total = total + steps works out the right side first, with the total so far, and then gives the name total the new value. Each pass builds on the value the last pass left.

    The total needs a value before the first line that reads it, so the programs here set it above the loop. A line total = 0 at the top of the body would wipe out the total at the start of each pass, and only the last value would be left at the end.

    An average is the total divided by the number of values. Dividing with / gives a float, so 20000 / 4 displays as 5000.0, with a decimal point. The short form count += 1 adds 1 to count and gives it the new value, much like count = count + 1.

    4 The rule

    A running total or a count is set to its starting value before the loop and changed inside the body, so each pass builds on the last.

    The biggest value so far starts with the first value and is replaced inside the loop when an if finds a bigger one. x += 1 adds 1 to x.

    5 How

    1. Decide what to keep: a total, a count or the biggest value.
    2. Before the loop, give it a starting value: 0 for a total or a count, the first value for the biggest. A line such as total = total + steps reads the name before it changes it, so the name needs a value first.
    3. Inside the body, change it on the passes that need it: add each value to a total, add 1 when a pass matches, or replace the biggest when an if finds a bigger value.
    4. After the loop, use the result: display it, or, when the count is above 0, divide the total by the count for an average.
    5. Trace two passes by hand and check that the value after each pass builds on the one before.

    6 Worked example

    Add up four days of steps

    Python code
    total = 0
    for day in range(4):
        steps = int(input())
        total = total + steps
    average = total / 4
    print(f"Total: {total}")
    print(f"Average: {average}")
    

    Standard input

    4000
    6500
    5500
    4000
    

    Standard output

    Total: 20000
    Average: 5000.0
    

    Type the four numbers one a line, or follow the table above. Each pass reads one number and adds it to the total. The average is worked out in a name of its own before it is displayed.

    Keep the biggest score so far

    Python code
    biggest = int(input())
    for turn in range(3):
        score = int(input())
        if score > biggest:
            biggest = score
    print(biggest)
    

    Standard input

    12
    40
    33
    40
    

    Standard output

    40
    

    The first score, 12, is the biggest so far. 40 replaces it. 33 and the second 40 are not bigger than 40, so biggest does not change, though score does.

    Count the letter a

    Python code
    count = 0
    for ch in "banana":
        if ch == "a":
            count += 1
    print(count)
    

    Standard input

    Empty

    Standard output

    3
    

    The count goes up by 1 only on the passes where the character is a, because the if runs its block only then. banana has three of them.

    Trace

    Trace the biggest-score program with the input 12, 40, 33 and 40
    StepscorebiggestExplanation
    Before the loop—12The first number read is the biggest so far.
    Pass 1404040 > 12 is True, so biggest becomes 40.
    Pass 2334033 > 40 is False, so biggest stays 40.
    Pass 3404040 > 40 is False, so biggest stays 40.
    print(biggest)4040After the loop, the line at the margin displays 40.

    7 The trap

    A total set to 0 at the top of the body starts again on every pass. With total = 0 as the body's first line, the step program would display Total: 4000, the last day's steps alone, and Average: 1000.0, that one day divided by 4. Put the starting value above the for line.

    Starting the biggest value at 0 goes wrong when every value is below 0: no value is bigger than 0, so the program would display 0, a number that was never read. Start with the first value instead, as the worked example does.

    Quick checks

    Quick check 1 of 2

    Predict what this program displays. The program reads no input.

    Python code
    total = 0
    for n in range(2, 11, 2):
        total = total + n
    print(total)
    Show the steps and the answer
    Answer

    It displays 30.

    Why: The range gives 2, 4, 6, 8 and 10. Adding them one a pass gives 2, 6, 12, 20 and then 30.

    Source: Python Software Foundation, Python 3.13 Library Reference, Built-in Types: Ranges

    Quick check 2 of 2

    Predict what this program displays. The program reads no input.

    Python code
    for n in range(1, 5):
        total = 0
        total = total + n
    print(total)
    Show the steps and the answer
    Answer

    It displays 4.

    Why: Each pass sets total to 0 and then adds that pass's number. After the last pass, with n holding 4, total is 4.

    Source: Python Software Foundation, Python 3.13 Language Reference, 8.3 The for statement

    Sources

    1. Python 3.13 Tutorial, 3.2 First Steps Towards Programming. Python Software Foundation. Python Software Foundation License Version 2. Read 2 Dec 2026.
    2. Python 3.13 Language Reference, 8.1 The if statement. Python Software Foundation. Python Software Foundation License Version 2. Read 2 Dec 2026.
    3. Python 3.13 Language Reference, 7.2.1 Augmented assignment statements. Python Software Foundation. Python Software Foundation License Version 2. Read 2 Dec 2026.
    4. Python 3.13 Language Reference, 8.3 The for statement. Python Software Foundation. Python Software Foundation License Version 2. Read 2 Dec 2026.
    5. Python 3.13 Tutorial, 3.1.1 Numbers. Python Software Foundation. Python Software Foundation License Version 2. Read 2 Dec 2026.
    6. Python 3.13 Library Reference, Built-in Functions: int. Python Software Foundation. Python Software Foundation License Version 2. Read 2 Dec 2026.
    7. Python 3.13 Library Reference, Built-in Functions: input. Python Software Foundation. Python Software Foundation License Version 2. Read 2 Dec 2026.
    8. Python 3.13 Library Reference, Built-in Types: Ranges. Python Software Foundation. Python Software Foundation License Version 2. Read 2 Dec 2026.