Answer to Question #278393 in Python for ben

Question #278393

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 



1
Expert's answer
2021-12-11T05:52:30-0500
def my_sqrt(a):


	if a <= 0:
		return None
	x = 1
	while True:
		y = (x + a/x) / 2.0
		if y == x:
			return x
		x = y


print(my_sqrt(1))
print(my_sqrt(10))
print(my_sqrt(16))

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!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS