我正在学习Python中的递归。然而,Python很奇怪:
def listsum(numslist, _total=0):
if len(numslist) > 1:
_total += numslist[-1]
print("Total is %d" % _total)
numslist = numslist[:-1]
listsum(numslist, _total)
else:
_total += numslist[-1]
print("Returning %d" % _total)
return _total
if __name__ == '__main__':
print(listsum([10, 10, 10]))
对我而言,它会返回:
Total is 10
Total is 20
Returning 30
None
有人能告诉我我做错了吗?
编辑(修复)
listsum(numslist, _total)
应该是
return listsum(numslist, _total)