I have converted the base program into two functions. I need to be able to exit the program by hitting the enter/return key but when I do, it throws a ValueError: could not covert string to float.
I have tried assigning the var(x) outside of the loop and also tried using an if statement to close but the issue seems to be with the float attached to the input. I'm wondering if I can move the float statement to another part of the program and still get the correct output?
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 = float(input("Enter a positive number or enter/return to quit: "))
print("The program's estimate is", newton(x))
print("Python's estimate is ", math.sqrt(x))
if name == 'main': main()
My expectation is that when the user hits the enter key, the program will end with no errors. The float is needed for the program.
File "C:/Users/travisja/.PyCharmCE2019.2/config/scratches/scratch.py", line 13, in main x = float(input("Enter a positive number or enter/return to quit: ")) ValueError: could not convert string to float: