Iterating over a list in a function


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

n = [3, 5, 7]

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


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)):


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