← Console bridge: text in, text out

Running total

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.

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.

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 = 0
while 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?

You type 4, then 5, then 3. What does the program show?

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 = 0
while 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 = 0
while 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.

typedtotal aftertotal < 10
44True
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.
Need a hint?

Start count at 0 next to total, before the loop.

Another hint

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

    This mixes the last lesson's loop with a value that grows, as in the spiral.

    lesson.py
    total = 0
    while total < 12:
        total += 5
    print(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.

    Try it yourselfYour edits are saved in this browser.
    Need a hint?

    Set total to 0 before the loop. Ask once before the loop and again at the end of every pass.

    Another hint

    Inside the loop: total += int(answer), then ask again.

    Keep asking for points until 0 is typed, then print the total.

      Run your program. This ticks when your code does all of the above.

      For teachers

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

      Open the teacher guide: answers, solutions, slides and a worksheet.

      Students will be able to

      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.

      The hard part (on purpose)

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

      When students get stuck

      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

      Now you can
      Next lesson8. Guess the numberRead a whole guessing game that uses loops, checks and a counter together, then change it and build a dice game from the same parts.