15/18 iterating over a list in a function


#1



Replace this line with your code.


#2

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.


#3

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


#5

Thank you - it has really helped me


#6

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


#7

Thank you or the explanation


#8

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