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


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


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


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