← Teacher guide

Running total

Students keep a running total with += inside a while loop, set once before the loop.

Console bridge: text in, text out · Keep asking · about 35 minutes · Open the student lesson

Classroom resources

Load these into Waypoints Prepare: choose Load URL and paste the copied address, or download the file and import it. In Prepare, the worksheet's Teacher edition shows the answers; the student copy hides them.

Students will be able to

Timing

Predict and Run 9 min · Investigate 9 min · Modify 5 min · Mix it up 3 min · Make 9 min

Running the lesson

  1. Predict: take a vote. The wrong options are two real beliefs: that += replaces the total, and that the loop stops at 9. Ask the 'replaces' voters what += does.
  2. Run: run with 4, 5, 3 and then with 20. Ask how many times the loop ran for 20, and why.
  3. Investigate: the bug is the counter reset. Ask students where the total is set to zero and how often that line runs.
  4. Make: students choose their own scoring theme. Early finishers print an average.

In class

On the slides at these points. Students can do the whole lesson on their own; these are for the room. In the deck, the Run, Modify and Make slides have the live playground in them (the tutorial link is the backup), and the notes above are in each slide's speaker notes.

The hard part (on purpose)

Putting total = 0 inside the loop. Ask when each line runs, and how often.

When students get stuck

What you will seeA question to askThe fix
The total prints only the last number.Where is the total set to 0, and how often does that line run?Move total = 0 above the loop so it runs once.
A NameError for total.Is total made before the loop uses it?total = 0 must come before the first line that reads total.

Answers to the checks

  1. Predict: You type 4, then 5, then 3. What does the program show?
    Total: 4, Total: 9, Total: 12, then Done. total += n adds n to what total already holds. After 9 the condition total < 10 is still true, so it asks again. 12 is not less than 10, so the loop ends.
  2. Follow the total: You type 4, 5, then 3. Fill in the total after each pass and whether the loop goes round again.
    9, True, 12, False
    Each answer is added to what was there. 4, then 4 + 5 = 9, then 9 + 3 = 12. 12 is not less than 10, so the loop ends.
  3. Spot the bug: The program should add up the scores, but it always prints the last score by itself. Click the line that is wrong.
    line 3
    total = 0 inside the loop wipes the total at the start of every pass. Set it to 0 once, before the loop, so it can build up.
  4. Mix it up: total starts at 0 and 5 is added on each pass while total < 12. How many passes does the loop make?
    3
    total goes 0, 5, 10, 15. 0 and 5 and 10 are all less than 12, so the loop runs three times. After 15 it stops.

Solutions

One possible solution for each task. Other correct programs are fine.

Modify

total = 0
count = 0
answer = input("Add a score, or 0 to finish: ")
while answer != "0":
    total += int(answer)
    count += 1
    answer = input("Add a score, or 0 to finish: ")
print("Total: " + str(total))
print("Scores: " + str(count))

Make

total = 0
count = 0
answer = input("Add a score, or 0 to finish: ")
while answer != "0":
    total += int(answer)
    count += 1
    answer = input("Add a score, or 0 to finish: ")
print("Total: " + str(total) + " from " + str(count) + " scores")

Stretch and support

Stretch: Print the average as well. What happens if the person types 0 straight away?

Support: Give the loop with the += line missing and ask for it.

Exit ticket

total is 7. What is total after total += 3, and what was the line short for?

10. total += 3 is short for total = total + 3.

What each step is for

Where it leads

Next lesson: a longer program, a guessing game, built from the moves so far. Read it, then change it.