You have amounts = [10.0, 25.5, 8.0]. You want their sum. How do you walk the list one item at a time?
Start at zero, add each item in turn?
That's the accumulator pattern. You initialize a total outside the loop, then a for loop hands you each item one at a time and you add each one to the running total:
total = 0.0
for amount in amounts:
total += amountWhat does for amount in amounts actually do, step by step?
It assigns each element to the name amount in turn. First pass: amount = 10.0. Second pass: amount = 25.5. Third pass: amount = 8.0. The loop handles the counting for you — no index needed.
And total += amount — is that the same as total = total + amount?
Exactly. The += is shorthand for "add the right side to the left in place." Same result, less typing. You'll see it constantly.
def total_expenses(amounts):
total = 0.0
for amount in amounts:
total += amount
return totalFor an empty list, the loop runs zero times and total stays at 0.0 — correct answer, no special case.
So the shape is: initialize → loop → return. The same three beats will work for counts, maxes, anything that folds a list into one value.
You just described the spine of Week 2 and most of Week 3. Once the accumulator shape is in your fingers, the rest are variations on the theme.
TL;DR: initialize → loop-and-update → return.
total = 0.0+=0.0 not 0?| Start value | total + 10.0 | Result type |
|---|---|---|
0 (int) | 10.0 | float (promoted) |
0.0 (float) | 10.0 | float (clean) |
Both work. Starting with the right type makes intent explicit — and empty-list returns match the expected type.
You have amounts = [10.0, 25.5, 8.0]. You want their sum. How do you walk the list one item at a time?
Start at zero, add each item in turn?
That's the accumulator pattern. You initialize a total outside the loop, then a for loop hands you each item one at a time and you add each one to the running total:
total = 0.0
for amount in amounts:
total += amountWhat does for amount in amounts actually do, step by step?
It assigns each element to the name amount in turn. First pass: amount = 10.0. Second pass: amount = 25.5. Third pass: amount = 8.0. The loop handles the counting for you — no index needed.
And total += amount — is that the same as total = total + amount?
Exactly. The += is shorthand for "add the right side to the left in place." Same result, less typing. You'll see it constantly.
def total_expenses(amounts):
total = 0.0
for amount in amounts:
total += amount
return totalFor an empty list, the loop runs zero times and total stays at 0.0 — correct answer, no special case.
So the shape is: initialize → loop → return. The same three beats will work for counts, maxes, anything that folds a list into one value.
You just described the spine of Week 2 and most of Week 3. Once the accumulator shape is in your fingers, the rest are variations on the theme.
TL;DR: initialize → loop-and-update → return.
total = 0.0+=0.0 not 0?| Start value | total + 10.0 | Result type |
|---|---|---|
0 (int) | 10.0 | float (promoted) |
0.0 (float) | 10.0 | float (clean) |
Both work. Starting with the right type makes intent explicit — and empty-list returns match the expected type.
Write `total_expenses(amounts)` that returns the sum of a list of floats. Empty list returns 0.0.
Tap each step for scaffolded hints.
No blank-editor panic.