[Tutor] Expecting a float but get an int
Phil
phillor9 at gmail.com
Sun May 23 20:22:06 EDT 2021
On 24/5/21 12:38 am, Dennis Lee Bieber wrote:
> Those lines are computing results using INTEGERS and then converting
> the integer result to a float.
>
> You should convert the integers to float BEFORE doing the computations
That's what I was attempting to do Dennis although it doesn't appear to
make any difference to the result. My original question refereed to an
error that didn't actually exist.
> if you want to be safe (though recent Python does treat "/" as a float
> division -- which always confuses me as so many other languages treat it as
> integer if both sides are integers), and the "a *" should be a float
> coercing the second equation to float.
> "a" and "b" are rather meaningless names
Perhaps not. The idea is to draw a line based on y = mx + b and points
along that line. At the time of posting my question, "m" was "a" in my
function. My confusion stems from the fact that the centre of the plane
is 0, 0 whereas the top left corner on a computer system is 0, 0. I'm
still working on translating between the two systems. If I simply wanted
to plot a line then I could use wx.Python's plot library. However,
that's not the object of this project, I'm just trying to satisfy my
curiosity with no real aim in mind.
> Consider:
>
>>>> def slope_intercept(point1, point2):
> ... slope = (point2[1] - point1[1]) / float(point2[0] - point1[0])
> ... intercept = (point1[1] - slope * float(point1[0]))
> ... return slope, intercept
Point taken, a list of tuples is a good idea.
--
Regards,
Phil
More information about the Tutor
mailing list