Iterating over a list in a function


#1



https://www.codecademy.com/courses/python-beginner-nzzVa/3/4?curriculum_id=4f89dab3d788890003000096


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


#2

Your result variable should not be inside the for loop.


#3

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?


#4

result = 0

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


#5

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