Iterating over a list in a function


Oops, try again. total([0, 3, 6]) returned 6 instead of 9

n = [3, 5, 7]

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


Your result variable should not be inside the for loop.


thanks but i thought it wasn't inside the loop as long as its lined up with the loop? if i remove the current indent won't it not be in the definition of the function?


result = 0

This is the line to which I am referring. Try to move it above the for loop.


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