Learn the Bisection Method by Finding the Square Root of a Number - Step 17

Tell us what’s happening:

How can I create an if statement that checks if ‘root is None’ ? Isin’t that what I wrote?

Your code so far

def square_root_bisection(square_target, tolerance=1e-7, max_iterations=100):
    if square_target < 0:
        raise ValueError('Square root of negative number is not defined in real numbers')
    if square_target == 1:
        root = 1
        print(f'The square root of {square_target} is 1')
    elif square_target == 0:
        root = 0
        print(f'The square root of {square_target} is 0')

    else:
        low = 0
        high = max(1, square_target)
        root = None
        
        for _ in range(max_iterations):
            mid = (low + high) / 2
            square_mid = mid**2

            if abs(square_mid - square_target) < tolerance:
                root = mid
                break

            elif square_mid < square_target:
                low = mid
            else:
                high = mid


# User Editable Region

     if root is None:
            print(f'Failed to converge within {max_iterations} iterations.')

# User Editable Region


Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36

Challenge Information:

Learn the Bisection Method by Finding the Square Root of a Number - Step 17

Hi,
Your if statement is correct. However, there is an issue with the indentation which is very important in Python. See how the other if statements are indented.
Good luck

absolutely i am also stucked here, i don’t know what’s wrong with it

Please open a new thread

If you have a question about a specific challenge as it relates to your written code for that challenge and need some help, click the Help button located on the challenge. This button only appears if you have tried to submit an answer at least three times.

The Help button will create a new topic with all code you have written and include a link to the challenge also. You will still be able to ask any questions in the post before submitting it to the forum.

Thank you.