How to Build a Running Total with a Python for Loop

Carrying a value forward, loop pass after loop pass.

  • Lesson 3 of 5
  • PRO
  • Ages 10+
  • 3 hands-on levels
See what PRO unlocksA one-time $49.99 purchase. Topics 1–3 are free.

The dragon explains

Building up a total

  1. The dragon: So far your loops repeated something. Now let's make a loop that BUILDS something up, one pass at a time.

  2. The dragon: gold = gold + 10, written INSIDE a loop, adds 10 every single time it runs.

    example.pyPython 3
    gold = 0for i in range(3):    gold = gold + 10    print(gold)
    Output10 20 30
  3. The dragon: Each pass starts from wherever the LAST pass left off. That's the whole trick — the value carries forward.

    example.pyPython 3
    gold = 0for i in range(3):    gold = gold + 10    print(gold)
  4. The dragon: If you only want the FINAL total, move print() outside the loop — so it runs once, after all the passes finish.

    example.pyPython 3
    gold = 0for i in range(3):    gold = gold + 10print(gold)
    Output30
  5. The dragon: Notice the indent difference. Inside the loop = every pass. Outside the loop = just once, at the end.

Quick check

Check your understanding

Say your answer out loud before reading the marked one. Every wrong option is a mistake real beginners make.

  1. Question 1 of 1

    gold = 0. This loop runs 3 times, adding 10 each time. Where does print(gold) go to show ONLY the final total?

    question.pyPython 3
    gold = 0for i in range(3):    gold = gold + 10    ???
    • Outside the loop, not indentedCorrect
    • Inside the loop, same as the line above it
    • It doesn't matter — indentation doesn't change what happens

    Why: Indented under the for, a line runs every pass. Un-indented, back at the loop's own level, it runs once — after the loop is completely finished. That's how you get one final number instead of three.

In the game

What you'll practice

Three levels in the game: one guided first try with the shape of the answer already there, then two to solve on your own.

  1. 1

    GuidedYour first try

    The dragon finds 10 gold coins on each of 3 trips. Keep a running total and print the total after each trip — not just the final number.

  2. +2

    PracticeTwo more, on your own

This lesson is part of PRO.

PRO is a one-time $49.99 purchase, no subscription, and unlocks this whole topic along with everything else. Topics 1–3 are free, start to finish.

Unlock this lesson with PRO

Stuck three times? The game offers the answer — unasked.