Question: Can someone please help me figure out the error and what needs to be revised? The following code and instructions are below. When I run
Can someone please help me figure out the error and what needs to be revised? The following code and instructions are below. When I run my code I'm still getting the following errors:
Custom TestIncomplete:
2 Test of program
Test Output
Enter a positive number or enter/return to quit: Traceback (most recent call last): File "nt-test-70f0e215", line 1, inimport newton File "/root/sandbox/newton.py", line 47, in main() File "/root/sandbox/newton.py", line 42, in main x = input("Enter a positive number or enter/return to quit: ") EOFError: EOF when reading a line
Test Contents
import newton assert(newton.newton(49) == 7.000000000000002) assert(newton.newton(32) == 5.656854250817683) assert(newton.newton(.5) == 0.7071067811873449)
Can you please modify the below code to satisfy the errors above?
# 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.
"""
#Define the function newton()
#This function expects the input number as an argument
#returns the estimate of its square root
import math
def newton(x):
# Initialize the tolerance and estimate
tolerance = 0.000001
estimate = 1.0
# Perform the successive approximations
while True:
estimate = (estimate + x / estimate) / 2
difference = abs(x - estimate ** 2)
if difference <= tolerance:
break
# Output the result
print("The program's estimate is", estimate)
print("Python's estimate is", math.sqrt(x))
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 == "": #if user presses "Enter" then exit the program
break # Otherwise, continue the process of allowing new numbers
x = float(x)
newton(x)
main()
These are the following instructions:
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 program should also include a main function that allows the user to compute the square roots of inputs from the user and python's estimate of its square roots until the enter/return key is pressed. 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.4142135623730951 Python's estimate is 1.41 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
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
