最近我在学习递归,我写了一个简单的递归函数来验证我的理解:
def hello(n):
if n == 1:
return 'hello'
else:
print('hello')
hello(n-1)
def returnhello():
return 'hello'
print(returnhello())
print()
print(hello(5))
这个的输出结果如下所示:
hello
hello
hello
hello
hello
None
为什么递归中的最后一个调用打印出 None 而不是 hello?我本来期望它能够打印出 5 个 hello。