所以我写了这个代码段,但是似乎没有用。
x=1
sum1=0
n=int(input("enter how long the series should be"))
print (x)
for a in range (1,n):
sum1=sum1+(sum1**a)
print(sum1)
a=a+1
print("the sum of the series is",sum1)
输出为:
enter how long the series should be5
1
0
0
0
0
the sum of the series is 0
>>>
注意,您不会x
在循环中的任何地方使用输入。那是因为sum1 ** a
应该x ** a
。另外,您想使用range(1, n+1)
,因为第二个参数必须比要产生的最大值大一个。
a
按自己的方式递增是无害的,但不是必需的。该for
循环本身更新的值a
在每次迭代。(a
在循环的顶部递增会导致问题,因为a
在计算中使用它时会产生错误的值。)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句