列表的阶乘我有一个数字的列表:查找号码
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
函数。
input_set = {1,2,3,4,5}。这不是一份清单,而是一本字典。 –
[在Python中递归生成n阶乘列表]的可能副本(https://stackoverflow.com/questions/40560108/recursively-generating-a-list-of-n-factorial-in-python) –
input_set = { 1,2,3,4,5}这不是一个列表或字典 –