我正在尝试创建一个函数,它接受一个参数(一个数字)并返回该数字的阶乘。
例如 f(5) 将返回 1*2*3*4*5
到目前为止我所拥有的是
def product(n, term):
"""Return the product of the first n terms in a sequence.
term -- a function that takes one argument
"""
k, total = 1, 1
while k <= n:
k, total = k + 1, total * term(k, 1)
return total
def factorial(n):
"""Return n factorial by calling product.
>>> factorial(4)
24
"""
return product(n, mul)
但是,是否有可能使该术语仅需要 1 个参数?