15/18 iterating over a list in a function


#1

Hi, I understand your answer completely i just don’t understand why this is the correct way to do it…

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

Is it because of the use of range()?

What if i didnt want to use range?


15/18 Iterating over a list in a function
#2

this depends on what it refers to?

then you don’t use range? This is possible, and in my opinion the better solution, creating a list with range and accessing by index using square brackets is not really needed, we can get the values directly by doing for i in numbers, then we can add i directly to result


#3

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