-1

I have python2.7 installed on ubuntu 16.04. It's showing strange behavior. If I check for (5/4), it will just display 1 as output.

for (6/7) it is showing 0

Can anyone help me to fix this?

screenshot of error

Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135

1 Answers1

1

Not a strange error at all. Python 2.7 does Integer division, so you will always get an integers as the result of the expressions you have named. You can view it this way if you want:

5/4=int(5/4)=int(1.25) = 1

And this is how you can understand the expression you have shown:

float(5/4)= float(int(5/4)) = float(1) = 1.0

you need to use floats:

5.0/4.0
FlyingTeller
  • 17,638
  • 3
  • 38
  • 53