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

            print(f"Failed to converge within {max_iterations} iterations.")
        else: 
             root is not None
             print(f'The square root of {square_target} is approximately {root}')```

Please Tell us what’s happening in your own words.

Learning to describe problems is hard, but it is an important part of learning how to code.

Also, the more you say, the more we can help!

Sorry I tried posting but it just posted the code not the description. I am trying to figure out what i need to do to pass the code I tried indenting and moving things around but doesnt seem to pass still?

I don’t know what the code is supposed to do, as you deleted the link to the step

Tell us what’s happening:

Tried to move code around but still encountering problem somewhere, I think indentation but not sure. Code wont pass.

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.")
        else: 
             root is not None
             print(f'The square root of {square_target} is approximately {root}')

        

# 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/128.0.0.0 Safari/537.36

Challenge Information:

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

The response I keep getting is “you should create an else clause”?

else: handles all other cases, so it doesn’t take a condition. But you have a random condition on the next line

Not understanding whay you mean by random condition the request is to say is not None?

I just got what you meant by random condition got rid of it now it passes.

1 Like