Python 当j==1时返回j=none的递归Py程序?

Python 当j==1时返回j=none的递归Py程序?,python,recursion,Python,Recursion,当j==1时返回j=None的递归Py程序?这是没有意义的,因为指定的基本情况j必须等于1,并且不会再次调用函数 import sys y=10 def decrease(j): if j==1: print('j =' + str(j) + '(1)') print('returning j') return j else: print('j =' + str(j) + '(not 1)') pri

当j==1时返回j=None的递归Py程序?这是没有意义的,因为指定的基本情况j必须等于1,并且不会再次调用函数

import sys
y=10
def decrease(j):
    if j==1:
        print('j =' + str(j) + '(1)')
        print('returning j')
        return j
    else:
        print('j =' + str(j) + '(not 1)')
        print('decreasing j')
        j = j-1
        print('calling decrease j')
        decrease(j)
y=decrease(y)
print('complete')
print(y)

您忘记了在第二个分支的末尾返回递减(j)


通常,当遇到函数返回的意外
None
时,首先检查所有分支是否以
return
语句结束。如果没有它,函数将返回
None

else
子句不会
返回任何内容,因此您将得到
None
。也许你的意思是
退货减少(j)
谢谢@AChampion这起作用了!