Trouble with Python sum function

In summary: Keep practicing and you'll get the hang of it.In summary, the conversation discusses the creation of a sum function in Python and the issues the speaker is having with their code. The expert summarizes that the while loop is not correctly storing the values being added to the total variable. After making the necessary corrections, the conversation concludes with the speaker thanking the expert for their help and expressing their gratitude for learning from their mistakes.
  • #1
mr.me
49
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
  • #2
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
 
  • #3
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.
 
  • #4
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.
 
  • #5


There are a few potential issues with the code provided. First, it is important to note that Python does indeed have a built-in sum function, so it is not necessary to create your own sum function. However, if you are trying to understand how the sum function works, it can be a useful exercise to try and create your own version.

In the code provided, there are a few areas that may be causing trouble. First, the line (a_list[counter] + total) does not actually assign the sum to a variable. It simply performs the calculation but does not store the result. To fix this, you could assign the sum to a variable, such as total, so that it can be used in the next iteration of the loop. Additionally, the line total = total + counter does not seem to be necessary and may be causing issues with the final sum.

A revised version of the code could look something like this:

def sum_list(a_list):
total = 0
for num in a_list:
total += num
return total

# testing the function
my_list = [3, 3, 3]
print(sum_list(my_list))

This code uses a for loop to iterate through each number in the list and add it to the total variable. It then returns the final sum. This approach is simpler and more efficient than using a while loop and a counter variable.

In conclusion, it is important to carefully consider the purpose and functionality of existing functions before attempting to create your own. Additionally, it is important to thoroughly test and debug your code to ensure it is functioning correctly.
 

1. Why is the sum function not working in my Python code?

The most common reason for the sum function not working is that the input is not in the correct format. The sum function requires an iterable object, such as a list or tuple, as its argument. If the input is not in the correct format, the function will not be able to perform the addition and will return an error.

2. How do I use the sum function to add a list of numbers in Python?

To use the sum function to add a list of numbers, you can simply pass the list as an argument to the function. For example, if you have a list of numbers called my_list, you can use sum(my_list) to get the sum of all the numbers in the list.

3. Can I use the sum function to add strings in Python?

No, the sum function can only be used to add numerical values in Python. If you try to use it on a list of strings, it will return an error. To concatenate strings, you can use the join method or the + operator.

4. How can I handle empty lists when using the sum function in Python?

If you have an empty list as the argument for the sum function, it will return 0 as the sum. However, if you want to handle empty lists in a different way, you can use a conditional statement to check the length of the list before using the sum function.

5. Is the sum function in Python inclusive or exclusive?

The sum function in Python is inclusive, meaning it includes the first and last element in the sum. For example, if you have a list with values [1, 2, 3], the sum function will return 6. If you want to exclude the last element, you can use slicing to get a subset of the list before passing it to the sum function.

Similar threads

  • Programming and Computer Science
Replies
3
Views
316
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
4
Views
3K
  • Programming and Computer Science
Replies
18
Views
1K
  • Programming and Computer Science
2
Replies
55
Views
4K
  • Programming and Computer Science
Replies
15
Views
1K
  • Programming and Computer Science
Replies
8
Views
954
  • Programming and Computer Science
Replies
16
Views
1K
Replies
3
Views
768
  • Programming and Computer Science
Replies
9
Views
2K
Back
Top