Step by step explanation of 'Iterating over a list in a function'?


#1

I would be very grateful if someone could take the time to explain exactly whats happening in the below code. I have it working, no problem, but I don't want to move on until I really grasp whats happening. The part that has me confused is the range. The list 'n' isn't really a list? its a range? and therefore, all that this code is doing is adding up the total (3+5+7 = 15) and adding a 1 to variable 'result' for the total length of the range 'n' (15)?

Am I correct in this? or am I missing something..

n = [3, 5, 7]

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

thanks for any input!


#2

A great place to start is the archived forums. For this topic, I'd suggest the Lists and Functions Forum. For example, there's a thread on the subject you're investigating: 15/18 Iterating over a list in a function.


#3

Thanks for the response James. I found that thread earlier and didn't think it was helpful... that's what I get for not reading the WHOLE thing....:{ My answer was at the bottom.

Thanks.