Iterating over a list in a function ///Why does it work ///


#1

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

Both of these work. i wish to know why in the top one the only way i can get it to work is with result += numbers[i]

The one on the bottom will only work if i use result = result + i
If i use += on the bottom one it wont run

If i use = numbers + i OR = results + i on the top one it also wont work
WHY?

numbers = [3,5,7]
def total(numbers):
    result = 0
    for i in numbers:
        result = result + i
    return result
print total (numbers)

#2

This is a value at index i.

Following is the value in numbers scope,


#3

What does that have to do with the question that was posted?


#4

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