可能重复:
Python中的因子函数
我需要在python中编写一个返回N!的程序,而不使用factorial函数.我有一个程序到目前为止,但我一直收到错误说,local variable "fact" is assigned but never used.fact = 1分配后如何使用?
from pylab import *
def factorial(n):
fact = 1
for i in range(n):
print("i = ", i)
fact = fact * i
print("The factorial of " + str(n) + " is: " + str(fact))
Run Code Online (Sandbox Code Playgroud)
In [37]: def fact(n):
fac=1
for i in range(2,n+1):
fac *=i
return fac
....:
In [43]: fact(5)
Out[43]: 120
In [44]: fact(6)
Out[44]: 720
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
537 次 |
| 最近记录: |