Iterating over a list in a function


#1

n = [3, 5, 7]

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

What does [i] after numbers do? I replaced it with just “numbers” without [i] and my code didn’t work…


#2

I recommend reading the documentation on iterators to better understand this concept:

https://docs.python.org/2/library/stdtypes.html#iterator-types


#3

Thank you!! I am reading it and trying to understand it! :blush:


#4

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