-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
  • 100,794
  • 21
  • 241
  • 231
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
  • 63,992
  • 20
  • 159
  • 237
0

Try using the round function

In [19]: round(6.79999999,1)
Out[19]: 6.8
kilojoules
  • 9,768
  • 18
  • 77
  • 149