递归
特性:
>.一递一归
>.终止条件 一般为:0 1 -1
#测试函数的返回值为函数
def test_recursion():
return test_recursion()
print(test_recursion()) RecursionError: maximum recursion depth exceeded
#案例:计算 3!(3*2*1=6)
res = 1
for i in range(1,4):
res *= i
print(res) #for循环解决
#用递归的方式解决
'''
x = 3 -> 3 * fact(2)
x = 2 -> 2 * fact(1)
x = 1 -> 1
'''
def fact(x):
if x == 1:
return 1
return x * fact(x - 1)
print(fact(3)) #6