Paul's Python CoursePPC

Running totals and counters

Not met yet

A running total starts at 0 before the loop and grows on every pass: total = total + amount. A counter is a running total that grows by 1. Both are set before the loop. Set inside it, they go back to 0 every pass.

Try it

main.py
total = 0
count = 0
for reading in range(100, 400, 100):
    total = total + reading
    count = count + 1
print(total / count)

Output

Press Run and what your program prints shows up here.

Easy to get wrong

  • Puts total = 0 inside the loop, or thinks it does no harm there, so the total resets every pass.
  • Thinks a total can be added to before it has been given a starting value.

Where it comes up