Why am i not finding the total in the list?
ranger = [15,30,45,50, 51,55,57,58]
for rannum in ranger: total = rannum +rannum
print(total)
Videos
Hello, so I am trying to understand how to sum values through a for loop within python so that I start with a value, and then add to it, and then add the next value to that, and then add the next value to that, and so on, going through all values in my list/folder. I think this would be referred to as a "cumulative sum". So let's say I have a list of numbers:
list = [1,4,6,3,7,5,2,8,9]
I want to iterate over each value in this list and add all values to the previous like so:
for value in list:
1 + value
sum = 1 + value
print(sum)or something like that? I tried this, and it just adds one to each value in the list, when I want a single final cumulative sum produced. I am really confused about if my logic is correct there. Is this how I would carry out this cumulative sum operation? I am trying to take 1, and then add 4, and then add 6 to that, and then add 3 to that sum, and then add 7 to that sum, and so on. I realize one could also just add all of these values at once, but I am trying to understand how to do this with a for loop. Thanks!
I'm given an array of ('string',2.0),('string',3.0) and I don't how how to sum up the floats only?