我尝试写个用递归计算数组之和程序,结果返回值却是None,我错在哪里了呢?
def recursion_sum(numbers, i=0, sum=0): if i < len(numbers): sum += numbers[i] i += 1 print(i, sum) recursion_sum(numbers, i=i, sum=sum) else: return sum numbers = range(101) print(recursion_sum(numbers))
在 recursion_sum(numbers, i=i, sum=sum) 之前加上 return
谢谢。
print(recursion_sum(numbers))
此处不需要再打印了
直接调就行:recursion_sum(numbers)