如何在 Python 中的 while 循环中增加值>?



我试图在图上的曲线下找到空间的面积。这是通过获得多个矩形的面积来实现的,这些矩形的底部不变,但高度递增。矩形位于给定用户的两个端点之间。从a点开始递增0.1,直到到达b点。我的问题是,如果我不能使用范围,我如何在while循环中递增x?我尝试使用+=位,所以x=a+=1,但这会产生语法错误。

print("Computing the area under the curve x^2 + x + 1")
a = int(input("Enter the left end point a: x ="))
b = int(input("Enter the left end point b: x ="))

base = 0.1
x = 0
while (a <= x <= b):
area = (x**2 + x + 1) * base
x += area

尝试递增左端点变量a,而不是定义和递增x

print("Computing the area under the curve x^2 + x + 1")
a = int(input("Enter the left end point a: x = "))
b = int(input("Enter the right end point b: x = "))
base = 0.1
total_area = 0
while (a <= b):
sub_area = (a**2 + a + 1) * base
total_area += sub_area
a += base

print("The area under the curve is " + str(total_area))
  1. x应该等于a,因为x的范围是从a到b
  2. 在每个循环中,必须让一个加0.1
  3. 设置一个新的变量来记录总面积,例如"total_area",如下所示
print("Computing the area under the curve x^2 + x + 1")
a = int(input("Enter the left end point a: x ="))
b = int(input("Enter the left end point b: x ="))


base = 0.1
x = a # 1
total_area = 0

while (a <= x <= b):
area = (x**2 + x + 1) * base
x += base # 2
total_area += area # 3

print(total_area)

最新更新