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.
SlidesDownload Present from the browser, or export to PowerPoint or PDF.
WorksheetDownload Print the student copy; the Teacher edition has the answers.
Students will be able to
I can keep a running total with +=.
I can say why the total starts at 0 before the loop.
I can write a loop that adds up what the person types.
Timing
Predict and Run 9 min · Investigate 9 min · Modify 5 min · Mix it up 3 min · Make 9 min
Running the lesson
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.
Run: run with 4, 5, 3 and then with 20. Ask how many times the loop ran for 20, and why.
Investigate: the bug is the counter reset. Ask students where the total is set to zero and how often that line runs.
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.
Add it up together(end, 4 min)
One person types scores, the other keeps a tally on paper. Does the program's total match yours?
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 see
A question to ask
The 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
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.
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.
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.
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
Investigate: A running total starts once and changes on every pass. The condition can read it.
Modify: A second counter can run beside the total: count adds 1 every pass.
Mix it up: A value that grows by the same amount each pass, with a loop that watches it: the same shape as the spiral.
Where it leads
Next lesson: a longer program, a guessing game, built from the moves so far. Read it, then change it.