Creating a summation function


#1

Why is it that my code below does not add up all the numbers but only prints 7?

#start
n = [3, 5, 7]

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

print total(n)
#end

thanks!


#2

Because you are resetting the value to zero each iteration. Only the last iteration is accumulated.


#3

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