[Tutor] Simple variable type question

Alan Gauld alan.gauld at btinternet.com
Fri Feb 5 19:45:30 CET 2010


"Antonio de la Fuente" <toni at muybien.org> wrote 

> the doctest for the slope function failed, because is expecting a
> floating point value and not an integer:

So convert it to a float.

> def slope(x1, y1, x2, y2):
>     result_slope = ((y2 - y1) / (x2 - x1)) * 1.0
>     return result_slope

       return float(result_slope)

Or just 

      return float(y2-y1/x2-x1)

You might want to use a try/except to catch a zero division error too?


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/



More information about the Tutor mailing list