小编Ale*_*Key的帖子

返回递归函数

我刚刚开始学习python(v3.2.3)并且return在这个函数中遇到了一个奇怪的问题:

def test(x):
    if x > 9 :
        test(x - 10)
    else:
        print('real value',x)
        return x

x = int(input())
y = test(x)
print('this should be real value',y)
Run Code Online (Sandbox Code Playgroud)

当我运行它时,我得到:

45
real value 5
this should be real value None
Run Code Online (Sandbox Code Playgroud)

但我预计:

45
real value 5
this should be real value 5
Run Code Online (Sandbox Code Playgroud)

我尝试return x在外面添加if,我得到了默认输入值.任何人都可以解释一下return有效吗?

python recursion return

38
推荐指数
3
解决办法
2万
查看次数

标签 统计

python ×1

recursion ×1

return ×1