I want to write a function that takes a list of numbers and returns the cumulative sum; that is, a new list where the ith element is the sum of the first i+1 elements from the original list. For example, the cumulative sum of [1, 2, 3]
is [1, 3, 6]
.
Here is my code so far:
def count(list1):
x = 0
total = 0
while x < len(list1):
if x == 0:
total = list1[0]
print total
x = x +1
else:
total = list1[x] + list1[x -1]
print total
x = x + 1
return total
print count([1, 2, 3, 4, 7])
However, it is not working.
Can you tell me what I am doing wrong? I worked on this for quite some time now.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…