Iterating over a list in a function


#1



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


Traceback (most recent call last):
File "python", line 9, in
TypeError: 'function' object has no attribute 'getitem'


I have no idea what went wrong with my code. Plz help me


n = [3, 5, 7]

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

print total[n]


#2

result = result + numbers(i)

to get a value of a list we use [] not ()

and here

total[n]

to call a function we use () not []


#3

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