我怎样才能用Python编写一个代码来使标志替代

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

Formula that I'm trying to solve with python

抱歉这么长(而且可能会造成混乱)。在上面你可以看到我试图用 Python 求解的公式的图像。在这里,用户应该输入一个值“X”,然后在程序结束时,应该出现该公式的总和。

指数一开始为 25,按 1 减 1,直到 1。被除数为 1,按 1 加 1,直到 25。第一个分数将与第二个分数相减,第二个分数将与第二个分数相加。第三个,依次类推。

#By now, I could only discover how to do this formula adding the values, but not intercalating          #between sum and subtraction. 

x = int(input("X: "))

total = 0
for i in range (25 + 1):
    exp = 25 - i
    div = i + 1
    sum = (x ** exp)/div
    total += sum
print(f"The total is {total:.2f} ")
python for-loop
1个回答
0
投票

要交换符号,请使用

-1**i
:

x = int(input("X: "))
total = sum(-1**i * x**(25-i) / (i+1) for i in range(25))
print(f"The total is {total:.2f} ")
© www.soinside.com 2019 - 2024. All rights reserved.