方法一:for循环
def factorialFunc(n):
x = 1
for i in range(1, n+1):
x *= i
return x
方法二:递归 5x4x3x2x1
def factorialFunc(n):
if n == 1:
return 1
else:
return n * factorialFunc(n-1)
方法三:reduce()配合lambda函数
from functools import reduce
def factorialFunc(n):
return reduce(lambda x,y:x*y, range(1, n+1))
方法四:math.factorial()直接求
import math
print(math.factorial(5))