编辑代码

def f(x):
    return 6 * x**3 + 3 * x**2 - 16

def bisection(a, b, eps):
    if f(a) * f(b) > 0:
        print("Error: f(a) and f(b) have the same sign!")
        return None
    while (b - a) / 2 > eps:
        c = (a + b) / 2
        if f(c) == 0:
            return c
        elif f(c) * f(a) < 0:
            b = c
        else:
            a = c
    return (a + b) / 2

a = 1
b = 2
eps = 0.01

root = bisection(a, b, eps)

if root is not None:
    print("The positive root is:", root)