Python Forum
Problem with a while loop - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Problem with a while loop (/thread-13051.html)



Problem with a while loop - Nassib - Sep-25-2018

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))



RE: Problem with a while loop - ichabod801 - Sep-25-2018

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.


RE: Problem with a while loop - ichabod801 - Sep-25-2018

Also, if you ever do want to check if something is an int, use:

if isinstance(n, int):