Question
Package Newtons method for approximating square roots (Case Study: Approximating Square Roots) in a function named newton. This function expects the input number as an
Package Newtons method for approximating square roots (Case Study: Approximating Square Roots) in a function named newton. This function expects the input number as an argument and returns the estimate of its square root. The script should also include a main function that allows the user to compute square roots of inputs until she presses the enter/return key
An example of the program input and output is shown below:
Enter a positive number or enter/return to quit: 2 The program's estimate is 1.4142135623746899 Python's estimate is 1.4142135623730951 Enter a positive number or enter/return to quit: 4 The program's estimate is 2.0000000929222947 Python's estimate is 2.0 Enter a positive number or enter/return to quit: 9 The program's estimate is 3.000000001396984 Python's estimate is 3.0 Enter a positive number or enter/return to quit
Current Code:
import math
def newton(n): # Initialize the tolerance and estimate tolerance = 0.00000001 estimate = 1.0
# Perform the successive approximations while True: estimate = (estimate + n / estimate) / 2 difference = abs(n - estimate ** 2) if difference
return estimate
def main(): while True: # loop until user presses enter number = input("Enter a positive number or press Enter to exit ") if number == '': # input is enter python return '' break number = float(number) estimate = newton(number)
# Output the result print("The program's estimate is", estimate) print("Python's estimate is ", math.sqrt(number))
if __name__ == '__main__': main()
Error i get:
Program produces correct output given input' 1 out of 2 checks passed. Review the results below for more details. Checks Test CaseComplete 1 Test of program Custom Test Incomplete 2 test of module Test Output Traceback (most recent call last): File "codevolve-test-b653d26c", line 4, in module> assert(newton.newton(32)5.656854250817683) AssertionError Test Contents import newton assert(newton.newton(49)7.000000000000002) assert(newton.newton (32)5.656854250817683) assert(newton . newton(5) = 0.7071067811873449)Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started