使用Python总结ñ只使用和while循环序列方面

问题描述 投票:0回答:1

我有询问用户对输入端,用于项的n个量,并且还他们所选择的值对于x此序列。

x^5/5 - x^7/7 + x^9/9 - x^11/11 + ...

我有麻烦占符号改变每隔一任,不能if语句任何使用。

任何帮助将不胜感激,谢谢!

到目前为止我的代码:

print(" ")
print("Please enter the number of terms you would like to sum up.")
print("The sequence is x^5/5-x^7/7+x^9/9-...")
print(" ")
n=int(input('Number = '))
print(" ")
print("Please enter the number for x.")
print(" ")
x=int(input('x = '))
#
nsum=0
for i in range(5,n+6,2):
    coefficient=1/i
    for j in range(5,i+1,2):
        coefficient=-1*coefficient
    nsum=nsum+coefficient*x**i
#
print(nsum)
python
1个回答
0
投票

您的序列显示第n项为正,如果n是奇数,阴性,如果是偶数。所以,你可以在你的for循环做到这一点。另外,在你的代码的用户输入N不等于项数:

nsum=0
for index in range(0, n):
    coefficient=(-1)**(index)
    i =  2*index + 5
    nsum=nsum+coefficient*x**i/i
© www.soinside.com 2019 - 2024. All rights reserved.