0

Python 新手,最近我一直在尝试递归。以下是我的代码:

def recursive_sum(n_n_l):
    total = 0
    for element in n_n_l:
        if type(element) == list:
            total += recursive_sum(element)
        else:
            total += element
    return total


nnl = [11, [12, 13], 14]
print(recursive_sum(nnl))

当我使用在线 IDE 时一切顺利,我得到了 50 的预期结果,但是在我的计算机终端上运行它时,我收到以下消息:

TypeError: unsupported operand type(s) for +=: 'int' and 'list'.

有什么需要更新的吗?目前在 python 3.7.5 上运行并使用 Atom 文本编辑器 vers。1.37.0 非常感谢任何帮助!

4

0 回答 0