Python 当j==1时返回j=none的递归Py程序?
当j==1时返回j=None的递归Py程序?这是没有意义的,因为指定的基本情况j必须等于1,并且不会再次调用函数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
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这起作用了!