Which fix removes the bug?
for n in [1, 2, 3]:
total = 0
total += n
print(total) # expected 6
- Use
total = total + nhere - Loop over
range(3)instead - Set
total = 0before the loop - Print total inside the loop body
The accumulator is reset on every pass, so only the last value survives.
Play Problem Solving ▸