Python Forum

Full Version: Problem with a while loop
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hey, i am having a problem with a while loop and i am a beginner in python. How could i do a while loop so that python understands it this way : While n is not an integer, add 1 to the value of m(so in that case m would become 2) and then recalculate everything.
Thanks!

R=float(7)
r=float(5)
m=float(1)
n=(r/(R-r))*m

if n==int:
    print('Voici la valeur de n désirée',n)
else:
    while n!=int:
        m+=1
    print("Voici les valeurs m et n (m,n)",(m,n))
n is never going to be an int. If it's a float, it stays a float, it never changes to an int.

The way to check for this is to see if it is close to it's integer version:

if abs(n - int(n)) < 0.00000001:
You don't want to check if the difference is zero, because there may be floating point errors. But those should be really small, so that's why you test that the difference is less than a small number.
Also, if you ever do want to check if something is an int, use:

if isinstance(n, int):