[Tutor] Expecting a float but get an int

Roel Schroeven roel at roelschroeven.net
Mon May 24 11:02:59 EDT 2021


Chris Roy-Smith schreef op 23/05/2021 om 10:22:
> try making the values for x1, x2, y1, y2 floats before calling the
> function. Python does integer math on integers. even making one value in
> each calculation a float will make python to do floating point math.

That's not correct anymore. In Python 3 (and optionally in Python 2, if 
you do 'from __future__ import division'), the division operator (/) 
yields a float even for integers. The floor division operator (//) on 
the other hand yields an integer for integer operands, a float if one of 
the operands is a float.


 >>> 4/3
1.3333333333333333
 >>> 4.0/3.0
1.3333333333333333
 >>> 4//3
1
 >>> 4.0//3.0
1.0

-- 
"Honest criticism is hard to take, particularly from a relative, a
friend, an acquaintance, or a stranger."
         -- Franklin P. Jones

Roel Schroeven



More information about the Tutor mailing list