Reputation: 19476
If I do:
width = 14
height = 6
aspect = width/height
I get the result aspect = 2
rather than 2.33. I'm new to Python and expected it automatically cast this; have I missed something? Do I need to explicitly declare a float?
Upvotes: 2
Views: 122
Reputation: 879611
There are many options:
aspect = float(width)/height
or
width = 14. # <-- The decimal point makes width a float.
height 6
aspect = width/height
or
from __future__ import division # Place this as the top of the file
width = 14
height = 6
aspect = width/height
In Python2, division of integers returns an integer (or ZeroDivisionError). In Python3, division of integers can return a float. The
from __future__ import division
tells Python2 to make division behave as it would in Python3.
Upvotes: 8