[英]Adding y-values of a function after finding them in python
I have a function -125.22*x**2 + 3213.43*x + 2010.8
and I want to find all the values of y for x=0 until x=25, and then add all the y-values together.我有一个 function
-125.22*x**2 + 3213.43*x + 2010.8
,我想找到 x=0 直到 x=25 的所有 y 值,然后将所有 y 值加在一起。 I tried this code but it only showed me the y-values:我尝试了这段代码,但它只显示了 y 值:
def f(x):
return -125.22*x**2+3213.43*x+2010.8
for x in range(1,26):
print(f(x))
you can create a temporary variable for the sum:您可以为总和创建一个临时变量:
def f(x):
return -125.22*x**2+3213.43*x+2010.8
sumy = 0
for x in range(1,26):
print("x :", x, " - y :", f(x))
sumy += f(x)
print("sum (y) :", sumy)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.