JIT — Intro to Data Science · Python Lab · Problem 15 of 30

Running total and average

A loop that finishes holding an answer instead of forgetting everything — and the several ways it can finish holding the wrong one.

A loop that ends holding an answer

Both loops so far printed something on each pass and then forgot it. Nothing survived. Ask them for the class total afterwards and there is nothing to ask.

This one carries a number forward. Each pass takes what the box is already holding, adds one more mark, and puts it back — so when the loop ends, the box holds the whole total.

New words — accumulator and counter An accumulator is a box that grows as a loop runs: total = total + mark.
A counter is the same idea with a fixed step: count = count + 1, which adds one for every pass regardless of what the pass contained.
Read either line right-hand side first, as you did in problem 2: work out the new value, then put it back in the same box.
Both boxes have to exist before the loop starts total = total + mark reads the box before it writes to it. On the very first pass there has to be something in there already, so the box is created and set to nought above the loop.
Starting it at anything other than nought quietly adds that much to your answer. Starting it inside the loop would reset it on every pass, and the total would end up holding nothing but the last mark.

Why sum() is not the point here

You already know sum(marks) from problem 7, and it is the right thing to use in real work. The pattern matters anyway, because most of what you will want to accumulate has no ready-made function: a running total of only the students who passed, a count of the missing entries, a tally per city. Every one of those is this same three-part shape — set a box to nought, add to it inside the loop, use it after.

Where a line sits decides how many times it runs A counter pushed back out to the margin is no longer inside the loop, so it runs once, when everything is over. The count comes out as 1 however many students there were — and 1 is a believable enough number that nobody queries it.

What you are building

Total: 457 Students counted: 6 Average: 76.16666666666667
Coming later Written as a function, this becomes something you can call on any list, any time, in one line — that is problem 16.

Build the programme

Four things have to happen, and the marks are already in place. Click them in an order that works.

Two questions settle it. What must exist before the loop can add to it? And what cannot be worked out until the loop has finished?

Your plan

The steps still to place

Your programme written out in full. Five pieces are missing. Three of them produce a number that is wrong by a believable amount, which is worse than a number that is obviously wrong.

Leave nothing on "choose…". A wrong pick does not always cause an error — sometimes it just prints something you did not expect, which is the harder kind of mistake to spot.

Run and read

Here is the whole programme. The marks are yours to change. Check the count against the number of marks you typed — that is the quickest way to catch a loop that is not doing what you think.

Your programme

Output

Nothing has run yet.