Keep a total across the passes of a loop with +=, setting it to 0 once before the loop starts.
A till adds up prices, a scoreboard adds up points and a fitness app adds up steps. Each one keeps a number that grows as the loop goes round.
What you should already know:
Print text· Show text on the screen with print, and join pieces of text into one line with +.
If and else· Make a program choose which lines to run, using if and else.
In the turtle courses a value could grow every pass, like size = size + 5. A console program does the same with numbers the person types: it adds each answer to a running total.
How this lesson works: Predict, Run, Investigate, Modify, Make
Predict: Read the code and say what it will do before you run anything. A wrong guess is fine: it shows you what to look for.
Run: Run it and compare the result with your prediction.
Investigate: Trace how each line of code connects to what you see.
Modify: Change the code on purpose and check the result.
Make: Build something new using what you have learned. Expect a few tries; that is normal.
Make your prediction first. Help opens once you've locked it in.
PredictRead the code and say what it will do before you run anything. A wrong guess is fine: it shows you what to look for.
Predict
lesson.py
total = 0while total < 10:
total += int(input("Add: "))
print("Total: " + str(total))
print("Done")
+= is the short way to write total = total + .... Read the program. What is shown?
RunRun it and compare the result with your prediction.
Run
Run it and type 4, 5 and 3. Then run it again with 20.
Line
What it does
Why
total = 0
Starts the total at zero
Once, before the loop. Put inside the loop it would start again every pass
while total < 10:
Keeps going while the total is small
The total itself decides when to stop
total += int(input("Add: "))
Asks for a number and adds it on
Same as total = total + int(...)
print("Total: " + str(total))
Shows the total so far
The total is a number, so it needs str
lesson.py
total = 0while total < 10:
total += int(input("Add: "))
print("Total: " + str(total))
print("Done")
Try it yourselfYour edits are saved in this browser.
InvestigateTrace how each line of code connects to what you see.
Investigate
lesson.py
total = 0while total < 10:
total += int(input("Add: "))
print("Total: " + str(total))
print("Done")
The total changes on every pass.
A total that never builds up.
You type 4, 5, then 3. Fill in the total after each pass and whether the loop goes round again.
typed
total after
total < 10
4
4
True
5
3
The program should add up the scores, but it always prints the last score by itself. Click the line that is wrong.
ModifyChange the code on purpose and check the result.
Modify
The program adds up scores until 0 is typed. Change it so it also counts how many scores were typed and prints the count.
Try it yourselfYour edits are saved in this browser.
Inside the loop add 1 to count, then print it after the loop.
Also count how many scores were typed, and print the count.
Run your program. This ticks when your code does all of the above.
InvestigateTrace how each line of code connects to what you see.
Mix it up
The total grows each pass, like the spiral did, and the loop keeps an eye on it.
lesson.py
total = 0while total < 12:
total += 5print(total)
total starts at 0 and 5 is added on each pass while total < 12. How many passes does the loop make?
MakeBuild something new using what you have learned. Expect a few tries; that is normal.
Make
Make a points counter. It asks for a score, adds it to the total, and asks again, until the person types 0. Then it prints the total and how many scores were added. If you've built a scoreboard on the micro:bit, this is the same job done with text.
Try it yourselfYour edits are saved in this browser.
Build up to it
Someone types these scores, one at a time. Fill in the total after each one.
I can say why the total starts at 0 before the loop.
I can write a loop that adds up what the person types.
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.
The hard part (on purpose)
Putting total = 0 inside the loop. Ask when each line runs, and how often.
When students get stuck
The total prints only the last number. Ask: Where is the total set to 0, and how often does that line run? Fix: Move total = 0 above the loop so it runs once.
A NameError for total. Ask: Is total made before the loop uses it? Fix: total = 0 must come before the first line that reads total.
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.
Where it leads
Next lesson: a longer program, a guessing game, built from the moves so far. Read it, then change it.
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.