-1

I have a few variables, x, y and result:

>>>x = 40
>>>y = 45
>>>result = x / y * 100
>>>result
0

So result == 0. I know result == 0, because I had to print x = 40.0 instead of print x = 40.

But anyway I need it to print out:

88.88888888888889

How can I do that?

Note: I can't just write x = 40.0?

And also I have tried to do this like this:

x1 = str(x) + '.0'
result = int(x1) / y * 100

But Python give me an error:

ValueError: invalid literal for int() with base 10: '40.0'

So how can I fix this?

0

1 Answer 1

1
>>> from __future__ import division
>>> x = 40
>>> y = 45
>>> result = x / y * 100
>>> result
88.88888888888889

In Python 2.7, / with integer arguments performs integer division. You can change this to use standard division with the import as shown.

Alternatively (if you don't want the behavior of / to be altered in this way), you can make one of the division arguments a float: float(x) / y * 100.