Question
Elena complains that the recursive newtonfunction in Project 2 includes an extra argument for the estimate. The functions users should not have to provide this
Elena complains that the recursive newtonfunction 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.
The Project 2 code is below:
# Modify the code below
"""
File: newton.py
Project 6.1
Compute the square root of a number (uses function with loop).
1. The input is a number, or enter/return to halt the
input process.
2. The outputs are the program's estimate of the square root
using Newton's method of successive approximations, and
Python's own estimate using math.sqrt.
"""
import math
# Initialize the tolerance
TOLERANCE = 0.000001
def newton(x, estimate):
"""Returns the square root of x."""
# Compute the difference and test for the base case
difference = abs(x - estimate ** 2)
if difference <= TOLERANCE:
return estimate
else:
# Recurse after improving the estimate
return newton(x, (estimate + x / estimate) / 2)
def main():
"""Allows the user to obtain square roots."""
while True:
# Receive the input number from the user
x = input("Enter a positive number or enter/return to quit: ")
if x == "":
break
x = float(x)
# Output the result
print("The program's estimate is", newton(x, 1))
print("Python's estimate is ", math.sqrt(x))
if __name__ == "__main__":
main()
if __name__ == "__main__":
main()
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