Question #189929

Part 1

Encapsulate the following Python code from Section 7.5 in a function named my_sqrt that takes a as a parameter, chooses a starting value for x, and returns an estimate of the square root of a. 

while True:

   y = (x + a/x) / 2.0

   if y == x:

     break

   x = y 


Expert's answer

def my_sqrt(a):
    x = a
    while True:
        y = (x + a/x) / 2.0
        if y == x:
            break
        x = y 
    return x

print('my_sqrt(2) =', my_sqrt(2))

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

LATEST TUTORIALS
APPROVED BY CLIENTS