-1

Im not sure if Im nuts or something, but look at this code:

a=1
while a<=2.2:
    if a==1.4:
        print "OK"
    a=a+0.4

output: "OK"

changed condition to 1.8:

a=1
while a<=2.2:
    if a==1.8:
        print "OK"
    a=a+0.4

output: nothing!?

Can anyone help me here?

MattDMo
  • 96,286
  • 20
  • 232
  • 224
Peter
  • 1

2 Answers2

1

Testing random floats for equality is usually a bad idea. It's probably something like 1.799999999.

Check out this link for more information.

Ethan Furman
  • 57,917
  • 18
  • 142
  • 218
0

Try using the round function

In [19]: round(6.79999999,1)
Out[19]: 6.8
kilojoules
  • 8,814
  • 17
  • 70
  • 133