小练习---递归求5!

  • 方法一
1 def fact(j):
2     sum = 0
3     if j == 0:
4         sum = 1
5     else:
6         sum = j * fact(j-1)
7     return sum 
8 print(fact(5))
  • 方法二
1 def Factorial(n):
2     if n == 1:
3         fn = 1
4     else:
5         fn = n*Factorial(n-1)
6     return fn
7 print(Factorial(5))

 

posted @ 2020-03-26 15:47  小他_W  阅读(310)  评论(0编辑  收藏  举报