Answer to Question #189943 in Python for FORTUNE AIDOO

Question #189943

Part 2

Write a function named test_sqrt that prints a table like the following using a while loop, where "diff" is the absolute value of the difference between my_sqrt(a) and math.sqrt(a). 

a = 6 | my_sqrt(a) = 2.44948974278 | math.sqrt(a) = 2.44948974278 | diff = 0.0

a = 7 | my_sqrt(a) = 2.64575131106 | math.sqrt(a) = 2.64575131106 | diff = 0.0

a = 8 | my_sqrt(a) = 2.82842712475 | math.sqrt(a) = 2.82842712475 | diff = 4.4408920985e-16

a = 9 | my_sqrt(a) = 3.0 | math.sqrt(a) = 3.0 | diff = 0.0 

Modify your program so that it outputs lines for a values from 1 to 25 instead of just 1 to 9. 

You should submit a script file and a plain text output file (.txt) that contains the test output. Multiple file uploads are permitted.



1
Expert's answer
2021-05-09T00:25:55-0400
import math
import sys
#important for correct operation it is necessary to insert 
#the implementation of the function my_sqrt (a)
def my_sqrt (a):
    pass


def test_sqrt():
    a = 1 
    while a <= 25:
        text_msg = (
            f'a = {a}| my_sqrt({a}) = {my_sqrt(a):.11f} |' 
            f'math.sqrt({a}) = { math.sqrt(a):.11f} |'
            f'diff = {abs(my_sqrt(a) -math.sqrt(a)):.11f}')
        print(text_msg)
        a += 1
        
# displaying the results on the screen        
test_sqrt()   
# example of outputting results to a file path_file = "test.txt" 
path_file = "test.txt"       
sys.stdout=open(path_file,"w")
test_sqrt()
sys.stdout.close()

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