Iterating over a list in a function - Error Want to Understand


#1


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

Error: "Oops, try again. total([0, 3, 6]) resulted in an error: list index out of range"

Why doesn't this work? Also, is(why is) print necessary after the for loop?

Can you explain what I'm asking my code to do which is causing the issue? I want to understand better.


n = [3, 5, 7]

def total(numbers):
    result = 0
    for item in numbers:
        print item
        result += numbers[item]
    return result


#2

your loop gets the values directly from the list, not indexes? so you can add item directly to result

its not necessary? but it might help you see what your code is doing


#3

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