Skip to content

Exercise 9 of 10 · Putting it together

Rainfall Week

What you will make

A seven-day rainfall chart with a row for every day, the week added up underneath it, and the wettest of the seven days picked out.

The one new idea: A variable made before a loop carries a running total from pass to pass

Adding up as you go, and holding on to the best you have seen, are the two patterns sitting underneath an enormous number of ordinary programs: a basket total, a high score, a longest streak, a progress bar. Each one is a variable made before a loop and changed inside it.

Go straight to the code ↓

A variable that outlives the loop

There is no new syntax in this exercise. A list, a for loop, an if, an f-string, + — you have met every piece of it. That is the point. Knowing five things separately is not the same as getting them to work together in one program, and the gap between those two is where most people stall.

The idea that joins them up is small. A variable made before a loop is still standing on every pass, and still standing after the loop ends, so whatever one pass leaves in it is what the next pass finds. Put the line that sets the starting value inside the loop instead, and that line runs again every time round, throwing away whatever the pass before it left there. Anything the loop must carry from one day to the next has to be started above it.

Two things are worth carrying that way.

A running total. Start a variable at zero above the loop, and inside the loop write total = total + mm. Python works out the right-hand side first — whatever the total was a moment ago, plus this pass's value — and puts the answer back under the same name. After seven passes it holds the sum of all seven.

The best so far. Start a second variable at zero above the loop, then inside the loop ask whether this pass beats it: if mm > wettest_mm:. When it does, store the new number. That variable then holds the largest value seen up to that moment — and by the final pass, the largest so far is simply the largest there is.

Remembering which day was wettest is one more line, not a new idea: inside the same if, store the day alongside the amount. They change together because they are one fact in two halves — the day that broke the record, and the amount that broke it.

A worked example

Four coins out of a jar, with the total printed on every pass so you can watch it climb:

Python
coins = [2, 5, 1, 5]
total = 0
biggest = 0
for coin in coins:
    total = total + coin
    if coin > biggest:
        biggest = coin
    print(f"coin {coin}  total {total}")
print(f"biggest coin: {biggest}")
Output
coin 2  total 2
coin 5  total 7
coin 1  total 8
coin 5  total 13
biggest coin: 5

total never drops back to zero, because the line that sets it to zero sits above the loop and runs once, before any counting starts. biggest ends on 5 — and notice it does not change on the last pass, even though that coin is a 5 too: 5 > 5 is false, so a tie leaves the first one in place.

Your turn

The editor holds a week of rainfall. rain is a list of seven measurements in millimetres, and the loop prints a row for each: a day number, a bar of one # per millimetre, and the measurement. Day two was dry, so its bar has no # in it at all and the row looks half empty. That is correct, not broken: zero millimetres draws zero hashes.

Press Run first. The chart is correct; the two lines beneath it are not. The total reads 0 mm, and the wettest day comes out as day 0, which is not one of the seven.

Both of your additions go inside the loop, lined up with the print above them.

  1. Add this day's rain to total.
  2. If this day beats wettest_mm, store both the new amount and the day number.

total, wettest_mm and wettest_day already exist above the loop, all holding zero: you are not making them, only changing them as the loop goes round. The counter day = day + 1 is a running total too, so the shape of your first line is already on screen.

If something goes wrong

The likeliest slip is indentation. A line that starts at the left margin is outside the loop, so it runs once, after the last day has gone by. A total that prints as 3 mm — day seven's rain and nothing else — is exactly that: the line needs four spaces in front of it.

If the millimetres come out right but the day stays at 0, the if is updating wettest_mm and forgetting wettest_day. Both lines belong inside the if, indented one step further than the if itself.

If Python stops with a SyntaxError mentioning two equals signs, the comparison was written with one. A single = puts a value into a name; the question you want here is a > anyway.

Nothing here can break. Once the week adds up, change one of the numbers in rain and run it again: the bar, the total and the wettest day should all move on their own.

Write your code

Runs in your browser. Press Run (or Ctrl/Cmd+Enter) and the output is checked for you.

Ctrl/Cmd+Enter to run

Press Esc then Tab to move keyboard focus out of the code editor.

Ready
Output will appear here after you run your code.

The runtime is starting in the background. You can type now — it will be ready before you are.

The answer appears here once you have run your code at least once.

Things that often go wrong here

Writing total = total + mm below the loop instead of inside it
A line that starts at the left margin sits outside the loop, so it runs once, after every day has already gone past. At that moment mm is still holding day seven's 3 from the final pass, so the total prints as 3 mm and openly disagrees with the chart above it. Four spaces in front of the line is the entire difference.
Setting total back to zero inside the loop
Put total = 0 on the first line of the loop and every day wipes out the days before it, so the whole week adds up to day seven's rain alone. A starting value belongs above the loop, where it happens once.
Remembering the amount but not the day
An if that updates only wettest_mm leaves wettest_day holding the zero it started with, so the last line claims the wettest day was day 0, which is not one of the seven. Both lines belong inside the same if, because they are two halves of one fact.
Writing if mm = wettest_mm
One equals sign puts a value into a name and two ask whether values match, and an if needs a question. Python will not run the file at all: it stops with SyntaxError: invalid syntax and suggests that you may have meant two equals signs. The test you want here is not equals anyway, but greater than.

Longer explanation: read the full lesson. Want a blank editor instead? Open the Python playground.