BugLab001 / 020up to 100
loopsindexingclassiceasy

Off By One

The function sum_list should return the sum of all elements in a list.

Something is wrong — it seems to miss the last element. Find the bug and fix it.

Don't use sum() built-in — you must iterate manually.

# your fix
+ must use for
- must not call sum()
hint 2after hint 1
hint 3after hint 2
solution

Read the code. Before running anything: what does the last line below give?

def sum_list(numbers):
total = 0
for i in range(len(numbers) - 1):
total += numbers[i]
return total
>>> sum_list([1, 2, 3])