# 15 iterating over a list or function


#1



Same plea, different exercise, what am I doing wrong?


Oops, try again. total([0, 3, 6]) resulted in an error: unsupported operand type(s) for +: 'int' and 'list'


n = [3, 5, 7]

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


#2

I know for loops can be confusing but try to keep it as simple as possible by using method 1 ,

for example in numbers:

then all you would need to do is add each iteration to result which is equal to 0,

result += item

And lastly return that result. :slight_smile:


#3

n = [3, 5, 7]

def total(numbers):
result = 0
for item in list:
result += item
return result

Oops, try again. total([0, 3, 6]) resulted in an error: 'type' object is not iterable

I am sorry to ask, but i am struggling to understand


#4

Your return statement should be on the same indentation level as your for loop,

for item in list:
    result += item
return result

#5

n = [3, 5, 7]

def total(numbers):
result = 0
for item in list:
result += item
return result
Oops, try again. total([0, 3, 6]) resulted in an error: 'type' object is not utterable

the first didn't copy right Im sorry can you please help some more


#6

His post was just an example. He wants you to do a variation about his code, so you should loop over n instead of list.


#7

The parameter is numbers which will be n when we pass it as the argument:

print total(n)

#10

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