老师布置的作业罢了:-(
1 import math 2 # 请在此处自定义函数 3 def f(x): 4 #此处为样例函数 sinx-x^2/4 5 return math.sin(x)-(x**2)/4 6 7 a = eval(input("请输入端点a:")) 8 b = eval(input("请输入端点b:")) 9 c = eval(input("请输入误差限:")) 10 result = 0 11 12 if f(a) * f(b) >= 0 or a >= b: 13 print('错误的范围') 14 exit() 15 16 # 开始求解 17 while True: 18 print('a={},b={}'.format(a,b)) 19 if b-a<=c: 20 print("b-a小于误差限,方程的根为:{}".format(a)) 21 break 22 mid = (a+b)/2 23 if f(mid) == 0: 24 print("方程的根为:" + str(mid)) 25 elif f(mid) * f(a) < 0: 26 b = mid 27 else: 28 a = mid