# Trouble with Python sum function

• mr.me

#### mr.me

I don't know why we have to write a sum function for python when there already is one but
this is supposed to sum a list with varible values and I don't understand why it doesn't work, can someone help explain there error, I believe that I am not giving proper values to the line that adds but I don't realize why<

## The Attempt at a Solution

Code:
def sum_list (a_list):
length = len(a_list)
counter = 0
total= 0

while(counter < length):

(a_list[counter] +total)

counter = counter + 1
total = total + counter

#testing the functions
my_list = [3,3,3]

print sum_list(my_list)

I don't know why we have to write a sum function for python when there already is one but
this is supposed to sum a list with varible values and I don't understand why it doesn't work, can someone help explain there error, I believe that I am not giving proper values to the line that adds but I don't realize why<

## The Attempt at a Solution

Code:
def sum_list (a_list):
length = len(a_list)
counter = 0
total= 0

while(counter < length):

(a_list[counter] +total) <<< Problem is here

counter = counter + 1
total = total + counter

#testing the functions
my_list = [3,3,3]

print sum_list(my_list)

Your while loop isn't right. Python evaluates a_list[counter] + total for each element in the array, but you aren't storing the value anywhere.

You need something like this:
total = total + a_list[counter]

Then get rid of the line that says total = total + counter

Thanks that's fixed.

To understand my mistake and clarify; is it fair to say that every time the loop ran it was adding 3 to zero then chucking that value into nothing as opposed to saving it, that's why the print was 3?

Again thankyou, I learn more here with a few of my mistakes and all your replies then my instructors teach.

Thanks that's fixed.

To understand my mistake and clarify; is it fair to say that every time the loop ran it was adding 3 to zero then chucking that value into nothing as opposed to saving it, that's why the print was 3?
Yes, that's exactly what was happening.
Again thankyou, I learn more here with a few of my mistakes and all your replies then my instructors teach.
You're welcome! We enjoy being able to help out.