我知道这个错误消息 'TypeError: 'NoneType' object is not iterable' 意味着没有数据。但是,我正在查看我的所有列表,并且没有任何部分没有值元素。这是与我的问题相关的代码部分。
def printList(heights):
print(" ".join(str(h) for h in heights))
def nextYear(heights):
next_heights = []
for i in range(len(heights)):
next_heights.append(heights[i] + 5)
i += 1
print(" ".join(str(h) for h in next_heights))
#main routine
heights = [33, 45, 23, 43, 48, 32, 35, 46, 48, 39, 41]
printList(heights)
print("Predicted heights after a year")
printList(nextYear(heights))
这是我的代码输出:
33 45 23 43 48 32 35 46 48 39 41
Predicted heights after a year
38 50 28 48 53 37 40 51 53 44 46
Traceback (most recent call last):
File "/Users/ellarendell/Desktop/test.py", line 17, in <module>
printList(nextYear(heights))
File "/Users/ellarendell/Desktop/test.py", line 2, in printList
print(" ".join(str(h) for h in heights))
TypeError: 'NoneType' object is not iterable
我希望我的代码在没有错误消息的情况下执行相同的输出。你知道列表的哪一部分可以有“无”吗?谢谢 :)
您的代码有两处错误:
next_heights
在你的函数中返回def nextYear(heights):
next_heights = []
for i in range(len(heights)):
next_heights.append(heights[i] + 5)
i += 1
return next_heights
如果没有返回行,它将返回None
并将其传递给printList
函数,您也不需要在内部打印,nextYear
因为您已经printList
在函数返回亮点后调用打印
for i in range(10):
print(i)
i +=15555
所以首先要做的是从循环中删除这一行
def nextYear(heights):
next_heights = []
for i in range(len(heights)):
next_heights.append(heights[i] + 5)
return next_heights
它会在每次迭代时自动增加,如果您希望它增加 2 而不是 1,那么您可以在range()
函数中将其指定为步长。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句