我試圖解決認爲Python演習10.3的Python:類型錯誤: '詮釋' 對象不是可迭代(認爲Python 10.3)
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]
.
我得到一個TypeError
與此代碼:
def culm_sum(num):
res= []
a = 0
for i in num:
a += i
res += a
return res
當我打電話culm_sum([1, 2, 3])
我得到
TypeError: 'int' object is not iterable
謝謝!
退房此線程,它有不止一個答案您的問題 http://stackoverflow.com/questions/494594/how-to-write-the-fibonacci-sequence-in-python –