131k views
3 votes
Encapsulate the following Python code from Section 7.5 in a function called 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:
a. y = (x + a/x) / 2.0
b. if y == x:
c. break
d. x = y

User JonnyRo
by
7.9k points

1 Answer

7 votes

Answer:

def my_sqrt(a):

while True:

x = a

y = (x + a / x) / 2

if (y == x):

break

else:

x = y

print(x)

return 0

my_sqrt(5)

Step-by-step explanation:

The above is a function in Python, and the exact copy as code of what is being given in the question. Its a method to find out the square root of a number which is a here. The while loop has been used as being mentioned in the question, and the variables are defined and calculated as being stated.

User Teena Thomas
by
8.5k points

Related questions