我有一组数字:
list = {1, 2, 3, 4, 5}
我希望创建一个函数,计算集合中每个数字的阶乘并打印出来。
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)
我看到的输出是:
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
这显然是错误的。我真的很想知道我的代码哪里出了问题,以及如何修复它。
注意:我不希望在此代码中使用math.factorial
函数。