Tell us what’s happening:
Not sure how to continue, I don’t thing there is enough information here to do the step?
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
# User Editable Region
for _ in range(max_iterations):
mid = (low + high) / 2
square_mid = mid**2
if tolerance in abs
# 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 13
Everything is here:
create an if
statement to check if the absolute value of the difference between square_mid
and square_target
is within the specified tolerance
.
create an if
statement
If statement needs to have a colon at the end of the line
to check if the absolute value of
The abs()
function returns the absolute value of a number,
abs()
is a function, you can look up how it works
https://www.w3schools.com/python/ref_func_abs.asp
the difference between square_mid
and square_target
Difference means you will need to use the -
operator to find the difference between these two variables
is within the specified tolerance
.
For example:
square_mid = 5
square_target = 10
tolerance = 2
square_mid - square_target = -5
abs(-5) = 5
(5 < tolerance) = False
Not within the tolerance in this case.
This is what I have now?
mid = (low + high) / 2
square_mid = mid**2
if num <= 0:
num = abs(square_mid - square_target) = tolerance```
I followed the suggestions by the help section it now reads the following and still not passing.
mid = (low + high) / 2
square_mid = mid**2
if abs(square_mid - square_target) < tolerance:```
Looking good! Remember the body of an if
can’t be empty, so you can use pass
as a placeholder for now
still not working with pass
mid = (low + high) / 2
square_mid = mid**2
if abs(square_mid - square_target) < tolerance:
pass
Keep your indentation consistent. You can’t have it all over the place like JS