Python Practice Example 26

Python 100例Python 100 Examples

Question:Use recursion to find 5!.

Program analysis:Recursion formula: fn=fn_1*4!

Program source code:

Example

#!/usr/bin/python # -*- coding: UTF-8 -*- def fact(j): sum = 0 if j == 0: sum = 1 else: sum = j * fact(j - 1) return sum print (fact(5))

The output of the above example is:

120

Python 100例Python 100 Examples

Other extensions