1

I have a basic loop as follows:

for x in range(81,346):
    print float(((x - 81)/(346-81))*100)
    # DO STUFF

The thing is, the output I get is 0.0 for all values. I checked to see that x-81 is giving the correct output. I also replaced 346-81 as 265. But for some reason, all output is being given as 0.0.
Any suggestions as to what is going wrong?

timgeb
  • 76,762
  • 20
  • 123
  • 145
Kanishka Ganguly
  • 1,252
  • 4
  • 18
  • 38

2 Answers2

4

You are performing integer division. Change 81 to 81.0 or cast the numerator or denominator to float.

timgeb
  • 76,762
  • 20
  • 123
  • 145
3

Python 2 uses integer division on integers. It was a bad idea and Python 3 fixed it. To have the (better) behaviour from future, put this in the beginning of your py file:

from __future__ import division
zvone
  • 18,045
  • 3
  • 49
  • 77