15/18 iterating over a list in a function


Replace this line with your code.


n = [3, 5, 7]

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

It says total [0,3,6] resulted in an error : 'int' object is not iterable.


have you indented properly? I'm stuck on the same exercise, so help would be appreciated


Thank you - it has really helped me


but now you don't understand why your code is wrong, there are two problems, sum() expects a list (or something it can loop over) and returns to you the result, for example:

print sum([0,1,2])

you can't loop over integers, so some() gives an error, so you will need a different way of adding to total

second problem, i contains the indexes of the list, not the actually numbers, you will need the index to get the item from the list to add to total


Thank you or the explanation


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