我有一个递归错误,我认为这是非常基本但我没有看到问题,虽然我认为它处于停止状态。 基本上,我有一个列表列表,每个列表是一个大约30点的楼层。我正在做一个递归到每个楼层并在每个楼层中制作一个表面多边形,然后我在另一个函数中使用每个surface_polygon进行挤压。
下面是代码和错误信息,他让我想要制作75个楼层但是一旦楼层列表为空他就错了。
def pisos_generico(list_lists):
if list_lists == []:
pass
else:
return surface_polygon (list_lists[0]) + pisos_generico(list_lists[1:])
def pisos (list_lists):
return extrusion(pisos_generico (list_lists)
这是错误代码:
Traceback (most recent call last):
File "D:\Universidade\3o Ano\PCA\Projeto\First Try Base Planta 2.py", line
547, in <module>
pisos (xy(0,0), 1.41, 0.02, 75)
File "D:\Universidade\3o Ano\PCA\Projeto\First Try Base Planta 2.py", line
529, in pisos
return extrusion(pisos_generico (piso_pisos_rodados (lista, n_andares, p, a, a_torcao)), vz(0.3))
File "D:\Universidade\3o Ano\PCA\Projeto\First Try Base Planta 2.py", line 503, in pisos_generico
return surface_polygon (list_lists[0]) + pisos_generico(list_lists[1:])
File "D:\Universidade\3o Ano\PCA\Projeto\First Try Base Planta 2.py", line 503, in pisos_generico
return surface_polygon (list_lists[0]) + pisos_generico(list_lists[1:])
File "D:\Universidade\3o Ano\PCA\Projeto\First Try Base Planta 2.py", line 503, in pisos_generico
return surface_polygon (list_lists[0]) + pisos_generico(list_lists[1:])
[Previous line repeated 72 more times]
TypeError: unsupported operand type(s) for +: 'surface_polygon' and 'NoneType'
更新版本:
def pisos (p, a, a_torcao, n_andares):
return pisos_generico (lista_listas)
def pisos_generico(list_lists):
if list_lists == []:
[]
else:
return [extrusion(surface_polygon (list_lists[0]), vz(0.3))] + pisos_generico(list_lists[1:])
新的错误消息,非常像旧消息:
Traceback (most recent call last):
File "D:\Universidade\3o Ano\PCA\Projeto\First Try Base Planta 2.py", line
548, in <module>
pisos (xy(0,0), 1.41, 0.02, 75)
File "D:\Universidade\3o Ano\PCA\Projeto\First Try Base Planta 2.py", line
530, in pisos
return pisos_generico (lista_listas)
File "D:\Universidade\3o Ano\PCA\Projeto\First Try Base Planta 2.py", line
503, in pisos_generico
return [extrusion(surface_polygon (list_lists[0]), vz(0.3))] +
pisos_generico(list_lists[1:])
File "D:\Universidade\3o Ano\PCA\Projeto\First Try Base Planta 2.py", line
503, in pisos_generico
return [extrusion(surface_polygon (list_lists[0]), vz(0.3))] +
pisos_generico(list_lists[1:])
File "D:\Universidade\3o Ano\PCA\Projeto\First Try Base Planta 2.py", line
503, in pisos_generico
return [extrusion(surface_polygon (list_lists[0]), vz(0.3))] +
pisos_generico(list_lists[1:])
[Previous line repeated 72 more times]
TypeError: can only concatenate list (not "NoneType") to list
非常感谢您的帮助
答案 0 :(得分:0)
如果您返回注释,函数将隐式返回None
。这发生在
def pisos_generico(list_lists):
if list_lists == []:
pass
else:
return surface_polygon (list_lists[0]) + pisos_generico(list_lists[1:])
# ^^^^^^^^^^^^
# empty => returns None
# exactly here on list_lists == [] is None returned
您可能需要以下内容:
def pisos_generico(list_lists):
if list_lists == []: # you can delete the ``... == []: pass``
pass # part and make the next one
elif len(list_lists) == 1: # a normal if instead of elif
return surface_polygon (list_lists[0]) # just make sure you do not call it with
else: # an empty list to start with
return surface_polygon (list_lists[0]) + pisos_generico(list_lists[1:])