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

Tell us what’s happening:

Had no idea why FCC didn’t pass my code I already put _ right in middle of for loop variable but nothing works

Your code so far


# User Editable Region

def square_root_bisection(square_target, tolerance=1e-7, max_iterations=100):
    if square_target < 0:
        raise ValueError("Cannot calculate square root of negative number")  
    low, high = 0, min(1,target)
    
    for _ in range(max_iterations): 
        mid = (low + high) / 2
        square = mid * mid

        if abs(square - square_target) < tolerance:
            return mid           
        if square < square_target:
            low = mid
        else:
            high = mid   
    return (low + high) / 2  

# 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 Edg/135.0.0.0

Challenge Information:

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

Welcome to the forum @galang20240100300

For that, inside the else block, create a for loop that runs up to max_iterations times.

For this step the for loop need to go in the else block.

Happy coding

why did they never tell us that , it was so confusing

that it has to go in the else block? it’s literally there

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.