python - 如何使用 For 循环查找 N 阶乘?

标签 python

<分区>

Possible Duplicate:
Function for Factorial in Python

我需要在不使用阶乘函数的情况下用 python 编写一个返回 N! 的程序。到目前为止,我已经编写了一个程序,但我一直收到错误提示,局部变量“fact”已分配但从未使用过。分配后如何使用 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))

最佳答案

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

关于python - 如何使用 For 循环查找 N 阶乘?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12810966/

相关文章:

根据字典值进行 Python 3D 绘图

python - Pycharm:预期类型 'Integral' ,取而代之的是 'str'

python - 我如何知道我的应用程序支持哪些版本的依赖项?

python Selenium 抓取 tbody

python - 从一组曲线到数据点的最佳拟合

python - 涉及循环引用时不执行 __del__method

Python。优化Append DF到MySQL DB

python 执行嵌套函数

python - 具有多个单词的 Django 全文搜索

python - 需要帮助解压缩存储在 Aztec 条形码中的 zlib 数据(德国铁路票)