Python:TypeError:无法解压不可迭代的 int 对象
问题描述
在数组中求大和的代码:
code for finding the large sum in the array:
def maxsum(arry):
if len(arry)==0:
return 0
summ,maximum_sum=0
for i in arry:
summ=summ+i
maximum_sum=max(summ,maximum_sum)
return maximum_sum
maxsum([1,2,-1,-2])
得到以下错误:TypeError
Traceback (most recent call last)
<ipython-input-5-59dc92538282> in <module>
7 maximum_sum=max(summ,maximum_sum)
8 return maximum_sum
----> 9 maxsum([1,2,-1,-2])
10
<ipython-input-5-59dc92538282> in maxsum(arry)
2 if len(arry)==0:
3 return 0
----> 4 summ,maximum_sum=0
5 for i in arry:
6 summ=summ+i
TypeError: cannot unpack non-iterable int object
解决方案
变量赋值错误.
def maxsum(arry):
if len(arry)==0:
return 0
summ,maximum_sum=0,0 # Changes
for i in arry:
summ=summ+i
maximum_sum=max(summ,maximum_sum)
return maximum_sum
https://note.nkmk.me/en/python-多变量值/
无法解压不可迭代的 int 对象:
cannot unpack non-iterable int object :
---------这是因为它试图将其转换为元组.
---------This is because it tried to convert it to tuple.
相关文章