这个问题已经提到过。我已经为它编写了一个代码,但我没有得到想要的输出。我们不必实际找到总和,只显示所有单独的加数,这就是我遇到问题的部分。
这是我写的代码:
x = int(input("Please enter the base number: "))
n = int(input("Please enter the number of terms: "))
s = 0
factorial = 1
for j in range(1,n+1):
factorial = factorial*j
for i in range(x,n+1):
s = (x**i)/factorial
print(s,end='+')
我的输出来了:
Please enter the base number: 2
Please enter the number of terms: 5
4.0+8.0+16.0+32.0+2.0+4.0+8.0+16.0+0.6666666666666666+1.3333333333333333+2.6666666666666665+5.333333333333333+0.16666666666666666+0.3333333333333333+0.6666666666666666+1.3333333333333333+0.03333333333333333+0.06666666666666667+0.13333333333333333+0.26666666666666666+
这显然不是我正在寻找的答案。我想要的输出是这样的:
Please enter the base number: 2
Please enter the number of terms: 5
2.0+2.0+1.33333+0.66667+0.26667+
我应该在代码中进行哪些更改以获得所需的结果?
- 旁注:我正在使用 Python 版本 3.8.5 的 Mac