Trouble with Python sum function

  • Context: Python 
  • Thread starter Thread starter mr.me
  • Start date Start date
  • Tags Tags
    Function Python Sum
Click For Summary

Discussion Overview

The discussion revolves around a user's difficulty with implementing a custom sum function in Python, despite the existence of a built-in sum function. The focus is on understanding the errors in the user's code and the logic behind summing a list of variable values.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Homework-related

Main Points Raised

  • The user expresses confusion about why a custom sum function is necessary when Python already has one.
  • The user attempts to implement a sum function but encounters an error, believing they are not providing proper values in the addition line.
  • One participant points out that the issue lies in the user's code where the sum is calculated but not stored, suggesting the correction to store the sum in the total variable.
  • The user seeks clarification on their mistake, asking if the loop was adding values but not saving them, which leads to an incorrect output.
  • Another participant confirms the user's understanding of the mistake, reinforcing the explanation provided earlier.

Areas of Agreement / Disagreement

Participants generally agree on the nature of the mistake in the user's code and the necessary correction. However, there is no broader consensus on the necessity of writing a custom sum function versus using the built-in one.

Contextual Notes

The discussion does not address potential limitations of the user's approach or the implications of using a custom function over the built-in function.

Who May Find This Useful

Readers interested in Python programming, particularly those learning about function implementation and debugging in coding exercises.

mr.me
Messages
49
Reaction score
0
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    
    return total      
#testing the functions
my_list = [3,3,3]print sum_list(my_list)
 
Technology news on Phys.org
mr.me said:
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    
    return total      



#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.
 
mr.me said:
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.
mr.me said:
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.
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 18 ·
Replies
18
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 4 ·
Replies
4
Views
4K
Replies
55
Views
7K
  • · Replies 15 ·
Replies
15
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 9 ·
Replies
9
Views
3K