概念:需要满足两个条件 1.引用自身 2.有一个最小可能性返回值
1 # n的阶乘 2 # 方法一(常规) 3 def factorial(n): 4 result = 1 5 for i in range(1,n+1): 6 result *= i 7 return result 8 9 if __name__ == '__main__': 10 result = factorial(4) 11 print(result)
1 # n的阶乘 2 # 方法二(递归) 3 def factorial(n): 4 if n == 1: 5 return 1 6 else: 7 return n*factorial(n-1) 8 9 if __name__ == '__main__': 10 result = factorial(6) 11 print(result)
标签:__,return,递归函数,factorial,print,result,阶乘 From: https://www.cnblogs.com/Happy-Girl-T/p/16836073.html