Problem 2: Factorial (100pts)
Write a function that takes a positive integer n and returns its factorial.
Factorial of a positive integer n is defined as
n!=n∏i=1i=1×2×3×⋯×n.
def factorial(n):
"""Return the factorial of a positive integer n.
>>> factorial(3)
6
>>> factorial(5)
120
"""
pass # YOUR CODE HERE
pass
的作用:正如冯老师在课上所说,pass
是一条“空指令”,表示什么都不做。通常,当我们定义一个函数,但没有想好函数怎么实现的时候,我们就会填入一个
pass
来保持程序结构的正确性。大家在完成lab和后续作业的时候,应该先把这一行pass
语句删除,然后再编写自己的代码,请不要在提交的代码中包含pass
。
Test your implementation with
python ok -q factorial
.