So, I'm trying to complete an exercise in URI Online Judge but I'm having trouble with checking if a float is an whole number. Here's my code:
I = 0.0
J = 1.0
while I <= 2:
for y in range(3):
if I.is_integer():
print("I=%d J=%d" % (I, J))
else:
print("I=%.1f J=%.1f" %(I, J))
J += 1.0
J -= 2.8
I += 0.2
Well, the output works fine until the last loop. In the first two loops where the values are whole numbers, it prints as it's supposed to: with floating point, because it's a whole number. But in the last loop, I get a whole number with a floating point, any ideas to fix this? Here's the output:
I=0 J=1 #working fine here
I=0 J=2
I=0 J=3
I=0.2 J=1.2
I=0.2 J=2.2
I=0.2 J=3.2
I=0.4 J=1.4
I=0.4 J=2.4
I=0.4 J=3.4
I=0.6 J=1.6
I=0.6 J=2.6
I=0.6 J=3.6
I=0.8 J=1.8
I=0.8 J=2.8
I=0.8 J=3.8
I=1 J=2 # also works fine here
I=1 J=3
I=1 J=4
I=1.2 J=2.2
I=1.2 J=3.2
I=1.2 J=4.2
I=1.4 J=2.4
I=1.4 J=3.4
I=1.4 J=4.4
I=1.6 J=2.6
I=1.6 J=3.6
I=1.6 J=4.6
I=1.8 J=2.8
I=1.8 J=3.8
I=1.8 J=4.8
I=2.0 J=3.0 # got messed up here
I=2.0 J=4.0
I=2.0 J=5.0
[EDIT] - Working as expected now
I = 0.0
J = 1.0
while I <= 2:
for y in range(3):
if I.is_integer() or I > 1.9:
if 1.9 < I < 2.1: # Made some workarounds here
I += 0.1
print("I=%d J=%d" % (I, J))
else:
print("I=%.1f J=%.1f" %(I, J))
J += 1.0
J -= 2.8
I += 0.2