'How to write a while loop until two outputs are the same ? [PYTHON]

I need to write a program that uses Newton's method to get an number's square root. Basically it needs to keep iterating until two consecutive values are the same. I think I am nearly there I just don't know what to put in the last part of the while loop.

def SquareRoot(X):

    root = X
    print(X)
       
    while root != DONT KNOW :
        root = 0.5*(root+(X/root))
        print(root)
        
SquareRoot(64)


Solution 1:[1]

you can do:

def SquareRoot(X):

    root = X
    print(X)
   
    while root**2 != X :
        root = 0.5*(root+(X/root))
        print(root)
SquareRoot(64)

but you can't do it for irrationals,

but to make it work you could round:

>>> def SquareRoot(X):
...     root = X
...     print(X)
...     while round(root**2,12) != X :
...         root = 0.5*(root+(X/root))
...         print(round(root,12))
...     return round(root,12)
...
>>> SquareRoot(2)
2
1.5
1.416666666667
1.414215686275
1.414213562375
1.414213562373
1.414213562373

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1