15. Iterating over a list in a function

#1

<Below this line, add a link to the EXACT exercise that you are stuck at.>

<In what way does your code behave incorrectly? Include ALL error messages.>

total([0, 3, 6]) returned 0 instead of 9

<What do you expect to happen instead?>

The exercise should be correct, no erros should occur.

This is my code. according to the forum this schould be the correct code, but it gives me the error anyways ;(

``````n = [3, 5, 7]

def total(numbers):
result = 0
for i in range(len(numbers)):
result = numbers[i] + result
return result
``````

#2

your return statement is inside the for loop, which means the loop will only iterate once before stopping. you need to move your return statement out of the for loop

#3
``````n = [3, 5, 7]

def total(numbers):
result = 0
for x in numbers:
result += x
return result

print total(n)
``````

thanks i accidently came to the solution, by trying the other method, and there, I intented correctly

#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.