小编Sak*_*ain的帖子

查找数字列表的阶乘

我有一组数字:

list = {1, 2, 3, 4, 5}
Run Code Online (Sandbox Code Playgroud)

我希望创建一个函数来计算集合中每个数字的阶乘并打印它.

input_set = {1, 2, 3, 4, 5}
fact = 1
for item in input_set:
    for number in range(1,item+1):
        fact = fact * number
    print ("Factorial of", item, "is", fact)
Run Code Online (Sandbox Code Playgroud)

我得到的输出是:

Factorial of 1 is 1
Factorial of 2 is 2
Factorial of 3 is 12
Factorial of 4 is 288
Factorial of 5 is 34560
Run Code Online (Sandbox Code Playgroud)

这显然是错的.我真的想知道我的代码有什么问题以及如何修复它.

注意:我不希望将此math.factorial函数用于此代码.

python math for-loop factorial

4
推荐指数
1
解决办法
4660
查看次数

标签 统计

factorial ×1

for-loop ×1

math ×1

python ×1