Iteration & Accumulation — Lesson 2: Add values as you go
New skill: Accumulator variable
Previously learned: Unit 05 — conditional execution
Why this matters
A running total begins at a starting value and changes every time a loop visits another item.
Study this example
total = 0
for number in range(1, 4):
total += number
print(total)
First total is 0. The loop adds 1, then 2, then 3, producing 6. += is shorthand for assigning the previous value plus a new amount.
Before moving on, predict the result of the program or function. Then run it and compare.
Check your understanding
What happens if the starting total is 10 instead of 0?
Common mistake
Initialize the accumulator before the loop. Inside the loop would reset it on every iteration.
Practice
Complete sum_to_limit(limit) to return the sum of every integer from 1 through limit, inclusive. limit is a nonnegative integer.
Public example 1
Input
sum_to_limit(4)
Output
10
Public example 2
Input
sum_to_limit(0)
Output
0
Public example 3
Input
sum_to_limit(1)
Output
1
Public example 4
Input
sum_to_limit(8)
Output
36
Hint 1
Start a total variable at zero.Hint 2
Remember the endpoint rule for `range()`.Hint 3
Update the total on each iteration and return it at the end.Reflection
Explain why your solution works and describe one boundary case you tried.