Skip to main content
 首页 » 编程设计

python之无类型错误,Python

2025年05月04日9EasonJim

我得到了

TypeError: 'NoneType' object is not iterable  

在这一行:

temp, function = findNext(function)  

并且不知道为什么会失败。我在 while 循环中使用函数:

while 0 < len(function): 
    … 

但我没有遍历它。 findNext(function) 中的所有返回值都差不多

return 'somestring',function[1:] 

并且无法理解为什么它认为我正在迭代其中一个对象。

请您参考如下方法:

我猜测 findNext 没有返回任何东西就掉到了最后,这使得它自动返回 None。有点像这样:

>>> def findNext(function): 
...     if function == 'y': 
...         return 'somestring',function[1:] 
... 
>>> function = 'x' 
>>> print(findNext(function)) 
None 
>>> temp, function = findNext(function) 
Traceback (most recent call last): 
  File "<stdin>", line 1, in <module> 
TypeError: 'NoneType' object is not iterable 

解决方案是始终返回一些东西。