I have simple task to count number, for example:
a = 7
b = 9
result = 0
for _ in (0:100):
result = a / b
b += 1
How I can stop the for loop when result is an integer?
Checking if method is_integer() wasn't meet my expectations.
I have to use Python 2.6
Use % 1. Modular arithmetic returns the remainder, so if you try to divide by 1 and the remainder is 0, it must be an integer.
if result % 1 == 0:
print "result is an integer!"
OR use the method mentioned in this post or this post:
if result.is_integer():
As mentioned in the comments, you can use:
while result % 1 != 0:
to make the loop repeat until you get an integer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With