Implement the Bisection Method - Implement the Bisection Method

Tell us what’s happening:

Implement the Bisection Method
I keep getting the error: You should set a default value for the tolerance and the maximum number of iterations (check 4).
The code works. All other checks complete.
But can’t get passed this check. It looks the same as code that others have produced. I can’t see what’s wrong.

Your code so far

def square_root_bisection(number, tolerance:float=.01, iterations:int=100):
    if iterations > 100:
        iterations = 100
    if number < 0:
        raise ValueError("Square root of negative number is not defined in real numbers")
        return None
    elif number == 0:
        print(f"The square root of {number} is {number}")
        return 0
    elif number == 1:
        print(f"The square root of {number} is {number}")
        return 1
    else:
        iteration = 0
        if number < 1:
            initial_test = 1 - (number / 2)
        elif number > 1 and number < 4:
            initial_test = 2.5
        elif number > 4:
            initial_test = number / 2
        if initial_test ** 2 == number:
            return initial_test
        else:
            while iteration <= iterations:
                if number > 1 and initial_test ** 2 > number:
                    high = initial_test
                    initial_test /= 2
                    low = initial_test
                elif number < 1 and initial_test ** 2 > number:
                    high = initial_test
                    initial_test /= 2
                    low = initial_test
                    if high - low < tolerance:
                        return (high + low) / 2 
                else:
                    if high - low < tolerance:
                        break
                    elif high * low > number:
                        high = (high + low) / 2
                    elif high * low < number:
                        low = (high + low) / 2 
                iteration += 1
            if high - low > tolerance:
                print(f"Failed to converge within {iterations} iterations")
            else:
                print(f"The square root of {number} is approximately {(high + low) / 2}")
                return (high + low) / 2

square_root_bisection(1, tolerance=5, iterations=10)
square_root_bisection(0.001, 1e-7, 50)
square_root_bisection(0.25, 1e-7, 50)
square_root_bisection(81, 1e-3, 50)
square_root_bisection(225, 1e-3, 100)
square_root_bisection(225, 1e-5, 100)
square_root_bisection(225, 1e-7, 10)






Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/26.5 Safari/605.1.15

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

Welcome to the forum @Jules2,

Try removing the :float and :int from your default assignments.

Happy coding