I'm starting out with learning Python 3.7, and wrote this very simple practice program for finding the square root of a number.
# User enters a number
x = int(input("Enter a number, any number: "))
# Start with a random guess (g) of 4
g = 4
# Check if g*g = x
# If not, redefine g as avg of g and x, square, then recheck
while g*g != x:
g = (g + x/g)/2
# When g*g = x, print g
else:
print(g)
For some numbers, the program works just fine. For others, however, the program simply doesn't resolve.
So, for example, if I run this program (in Spyder) and enter 56, it returns a value of 7.483314773547883. If I enter 66, the console doesn't return an output. It simply idles until I interrupt or reset it.
I also tried changing the while condition to:
while g*g not in range(int(x - 0.1), int(x + 0.1)):
But this threw up the same problem. I'd appreciate any advice on what the issue might be here.