Implement the Bisection Method - Implement the Bisection Method

Tell us what’s happening:

it’s been a few days since I work with implement the Bisection Method. tried so many but always failing in the end. til I figure out a close functioning code but step 8 and 9 aren’t working. So i’m wondering if anyone could with this problem, please and thank you!

Your code so far

def square_root_bisection(number, tolerance=1e-7, maximum=100):
    if number < 0:
        raise ValueError("Square root of negative number is not defined in real numbers")
        
    if number == 0 or number == 1:
        print(f"The square root of {number} is {number}")
        return number
    low = 0
    high = max(1, number)

    for _ in range(maximum):
        root = (low + high) / 2
        square = root * root

        if abs(square - number) <= tolerance:
            print(f"The square root of {number} is approximately {root}")
            return root
        
        if square < number:
            low = root
        else:
            high = root

    print(f"Failed to converge within {maximum} iterations")
    return None
    

Your browser information:

User Agent is: Mozilla/5.0 (X11; CrOS x86_64 14541.0.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/150.0.0.0 Safari/537.36

Challenge Information:

Implement the Bisection Method - Implement the Bisection Method

GitHub Link: freeCodeCamp/curriculum/challenges/english/blocks/lab-bisection-method/686ccc2c8b967e17ab18d593.md at main · freeCodeCamp/freeCodeCamp · GitHub

Hi @brandoncoder22,

…if the tolerance is 0.01, the bisection method will keep halving the interval until the difference between the upper and lower bounds is less than or equal to 0.01

Is that what you are checking for here?

Happy coding

Now I get it, thank you for your help.