上面的问题是我得到的"没有"作为我的输出
#defining two functions
f1 = lambda x: 2*x + 3
g1 = lambda x: 3*x - 4
# making a function called "intersect"
# a and b is the upper and lower limit
# eps is the tolerance of the answer
def intersect(f,g,a,b,eps):
c1= 0 #defining c1 (real number) for simplicity
n = 1 # to limit the number of iteration
while n <= 1000:
c1 = (a+b)/2 #using bisection formula
if abs(f(c1) - g(c1)) < eps or ((b-a)/2)<eps: # two conditions |f(c)-g(c)| <eps
break
return c1
if f(c1)<=0: # accd to bisection theorem
a = c1
else:
b = c1 # accd to bisection theorem
n +=1 # incrementing to limit the iteration
print(intersect(f1,g1, -10, 10, 0.001))
答案 0 :(得分:1)
问题在于:
if abs(f(c1)-g(c1)) < eps or ((b-a)/2) < eps:
break
return c1
break
语句在返回值之前退出while
循环,并且由于没有返回其他值,函数的返回值为None
。
返回值后,函数退出,因此您不需要break
语句。