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 0 instead of 9

I'm trying to use the length() function to compete this exercise but i don't know how to do it... help?

n = [3, 5, 7]

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


#2

Hi

this is an indentation problem. The line return result is too indented and should be at the same level as the line result = 0

In the current state of the code, the loop is executed only once and then the function returns result. So you get the first value of numbers.

Cheerio


#3

Try this:

for i in range(0, len(numbers)):

this helped me