Python Trouble with Python sum function

AI Thread Summary
The discussion centers on a user's confusion regarding their custom sum function in Python, which fails to accumulate values correctly. The main issue identified is that the code does not store the result of adding the current list element to the total, leading to incorrect output. The correct approach involves updating the total with each element in the list using the expression `total = total + a_list[counter]`. The user acknowledges their misunderstanding and appreciates the clarification, noting that they learn more from community feedback than from instructors. The conversation highlights the importance of proper variable assignment in iterative processes.
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.
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...

Similar threads

Replies
18
Views
1K
Replies
4
Views
3K
Replies
4
Views
4K
Replies
15
Views
2K
Replies
3
Views
1K
Replies
9
Views
2K
Back
Top