Austin Austin - 7 months ago 15
Python Question

How do I find the square root of the negative integer -16 or the numbers that it is a multiple of with a while loop

I am more interested in finding out why this doesn't work

x = -16
ans = -1
ans2 = 1
while (ans*ans2 > x):
ans = ans - 1
ans2 = ans + 1
print ans, ans2

Answer

You have a small typo in your code:

x = -16
ans = -1
ans2 = 1
while (ans*ans2 > x):
    ans = ans - 1
    ans2 = ans2 + 1    # changed ans with ans2
print ans, ans2

Output:

4 -4
Comments