Iterating over a list in a function


#1

This is my code:

n = [3, 5, 7]

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

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

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


#2

Could you please write the link of the lesson here ?


#3

the moment a return keyword is reached, the function ends. Which happens in the first run of your loop, you might want to re-indent the return so your whole for loop can run


#4

You just need to make spaces to be like this:

n = [3, 5, 7]

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

#5

Thank you!!! ............................................