Question: This answer needs to be in python Elena complains that the recursive newton function in Project 2 includes an extra argument for the estimate. The
This answer needs to be in python
Elena complains that the recursive newton function in Project 2 includes an extra argument for the estimate. The functions users should not have to provide this value, which is always the same, when they call this function. Modify the definition of the function so that it uses a keyword argument with the appropriate default value, and call the function without a second argument to demonstrate that it solves this problem.
My code
import math
tolerance = 0.000001
def newton(n, estimate= 1.0): new_estimate = (estimate + n / estimate) / 2 difference = abs(n - new_estimate ** 2) if difference <= tolerance: return new_estimate else: return newton(n, new_estimate)
def main(): while True: number = input("Enter a number or press * to exit ") if number == '*': break number = float(number) estimate = newton(number)
print("The program's estimate is ", estimate) print("Python's estimate is ", math.sqrt(number))
if __name__ == "__main__": main()
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
