Iterating over a list in a function


#1


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


n = [3, 5, 7]

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


#2

Your function is accessing the global object, n, not the local one, numbers. It will always give the same answer no matter what is passed in.

for i in range(len(numbers)):

#3

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