Tell us what’s happening:
Hi, I’m stuck on the final step as I have no idea what the logic is supposed to be here. (Like how are we expected to interpret this 1e-7 nonsense…)
I’ve made the code print out the result of every iteration and realise that if I change my tolerance to anything with more than 2 decimal places, the entire thing breaks and does not run and it completely stumps me. I must’ve overlooked something for sure. Can anyone help? ![]()
![]()
Your code so far
def square_root_bisection(number,tolerance=0.01,iterations=1):
if number<0:
raise ValueError('Square root of negative number is not defined in real numbers')
elif number==0 or number==1:
print(f"The square root of {number} is {number}")
return number
else:
results=''
lower_bound=0
if number>1:
upper_bound=number
else: #number<1
upper_bound=1
mid=0
for i in range(iterations):
if upper_bound-lower_bound>tolerance:
mid = (upper_bound+lower_bound)/2
square=mid**2
results+=f'{i+1} : Root:{mid} High:{upper_bound} Low:{lower_bound} Difference: {upper_bound-lower_bound}\n\n'
if square!=number:
if square<number:
lower_bound = mid
if square>number:
upper_bound = mid
else:
print(f'The square root of {number} is approximately {mid}')
print(results)
return mid
else:
print(f'The square root of {number} is approximately {mid}')
print(results)
return mid
square_root_bisection(9,0.1, 10)
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/148.0.0.0 Safari/537.36 Edg/148.0.0.0
Challenge Information:
Implement the Bisection Method - Implement the Bisection Method