【发布时间】:2012-10-10 16:44:25
【问题描述】:
我需要在 python 中编写一个返回 N! 的程序,而不使用阶乘函数。到目前为止,我已经编写了一个程序,但我一直收到错误消息,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))
【问题讨论】:
-
您帖子中的缩进与文件中的缩进是否相同?尝试将整个
for块缩进到函数定义之下。
标签: python