[Tutor] Expecting a float but get an int
Alan Gauld
alan.gauld at yahoo.co.uk
Sun May 23 04:21:19 EDT 2021
On 23/05/2021 08:28, Phil wrote:
> Help please.
>
> This function returns "a" as .7 and "b" as 0 instead of .7. I'm sure the
> reason is obvious but I cannot see what it is.
>
> def slope_intercept(x1, x2, y1, y2):
> a = (y2 - y1) / (x2 - x1) # a = .7 which is correct
It is not correct.
It should be (using your values) 0.7777777....
Or more accurately it is the rational number 7/9
> b = (y1 - (a * x1) # b = 0 where it should be .7
And this is 7 - (7/9) * 9 = 7-7 = 0
> slope, intercept = slope_intercept(9, 0, 7, 0)
The real mystery is how you get 0.7 for a...
> What have I overlooked?
Math.
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos
More information about the Tutor
mailing list