[Tutor] Expecting a float but get an int

dn PyTutor at DancesWithMice.info
Sun May 23 04:40:33 EDT 2021


On 23/05/2021 19.28, Phil wrote:
> 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
>     #a = .7
> 
>     b = (y1 - (a * x1) # b = 0 where it should be .7
>     return a, b
> 
> slope, intercept = slope_intercept(9, 0, 7, 0)
> print("slope :", slope, "intercept :", intercept)
> 
> This is what I've tried:
> 
>     a = float((y2 - y1) / (x2 - x1))
> 
>     b = float((y1 - (a * x1))
> 
> In the function:
> 
> x1 = float(x1)
> 
> the same for x2, y1, y2
> 
> The debugger show "a" and "b" to be floats.
> 
> The only way that I can have "b" show the correct answer is if I
> specifically make a = .7.
> 
> What have I overlooked?

Have you confused which parameter corresponds to the Cartesian
coordinates, eg "slope_intercept(9, 0, 7, 0)" is operating on the
coordinates (9, 7) and (0, 0). Doesn't any line mentioning the origin
have zero as its intercept? (it's late here and I'm sleepy)

The way to test is to add more print statements, even "print( a * x1 )"
to be sure that the small component of the calculation is working to spec!

If you are convinced it is a typing problem, then print the type( object
) value to be sure, maybe even adding typing meta-data to the parameters...
-- 
Regards =dn

-- 
Regards,
=dn


More information about the Tutor mailing list