1

I'm trying to program a function to compute Newton's method. Expect I keep getting an error in my code. This is the prompt that I was given to write a code for

And this is my code that I have written down

import math

def newton(x):
   tolerance = 0.000001
   estimate = 1.0
   while True:
        estimate = (estimate + x / estimate) / 2
        difference = abs(x - estimate ** 2)
        if difference <= tolerance:
            break
   return estimate

def main():
   while True:
       x = input("Enter a positive number or enter/return to quit: ")
       if x == '':
           break
       x = float(x)
       print("The program's estimate is", newton(x))
       print("Python's estimate is     ", math.sqrt(x))
main()

And it seems to be working but I keep getting this error when I run checks on Cengage

I'm not really sure what it means because my code seems to be running just fine. Can anyone help explain this?

Jianna
  • 59
  • 2
  • 8
  • Can you replace the print statements with this format: ```print("The program's estimate is " + str(newton(x)))``` – kerwei Mar 19 '19 at 02:22

2 Answers2

0

The issue seems to occur when the input is blank. A potential workaround, assuming you would only want positive numbers as input, would be to set a negative number (or anything else of your choice), like -1 for example, as an exit condition:

x = input("Enter a positive number or enter/return to quit: ")
if not x:
    break
x = float(x)

This should avoid the EOFError.


Edit

If you want to use a blank input (hitting the return line) to break out of the loop, you can try this alternative syntax:

x = input("Enter a positive number or enter/return to quit: ")
if not x:
    break
x = float(x)

The not x checks if x is blank. It is also more pythonic than x == "". Additional methods to detect a blank input are in this post as well: How do you get Python to detect for no input.

BusyProgrammer
  • 2,783
  • 5
  • 18
  • 31
  • Now the error read as this Enter a positive number or enter/return to quit: Traceback (most recent call last): File "newton.py", line 20, in main() File "newton.py", line 15, in main. x = float(input("Enter a positive number or enter/return to quit: ")) ValueError: could not convert string to float: – Jianna Mar 19 '19 at 02:05
  • How did you come to the conclusion that it's due to blank input? – kerwei Mar 19 '19 at 02:09
  • @kerwei In the test output, when a blank line is entered as input, the program encounters the EOF error. It works fine in other cases – BusyProgrammer Mar 19 '19 at 02:10
  • @ABusyProgrammer I ran the script locally on the py36 environment and it seems fine – kerwei Mar 19 '19 at 02:11
  • @kerwei Same here, I'm running it on the py37 environment and it works, but apparently its not running on Cengage – BusyProgrammer Mar 19 '19 at 02:13
  • @ABusyProgrammer when the input is blank the error reads `ValueError: could not convert string to float:` – Jianna Mar 19 '19 at 02:28
  • @Jianna If you are using the code in my answer, then a blank line does not represent any float number. If you try "-1", then the program should work fine. – BusyProgrammer Mar 19 '19 at 02:35
  • @ABusyProgrammer but my assignment requires that I allow the user to break the code using the return key – Jianna Mar 19 '19 at 02:44
  • @Jianna Updated my answer – BusyProgrammer Mar 19 '19 at 18:41
0

I did mine like this and Cengage accepted it.

import math

tolerance = 0.000001
def newton(x):
   estimate = 1.0
   while True:
        estimate = (estimate + x / estimate) / 2
        difference = abs(x - estimate ** 2)
        if difference <= tolerance:
            break
   return estimate

def main():
   while True:
       x = input("Enter a positive number or enter/return to quit: ")
       if x == "":
           break
       x = float(x)
       print("The program's estimate is", newton(x))
       print("Python's estimate is     ", math.sqrt(x))
if __name__ == "__main__":
    main()
Chase
  • 5
  • 6