[Tutor] Simple variable type question
Luke Paireepinart
rabidpoobear at gmail.com
Fri Feb 5 19:39:55 CET 2010
You want to promote one of your variables to float before you do the
calculation, this will make all other variables automatically cast. So
(float(y2) - y1) / (x2-x1). if you do the float cast after the
calculation, you will do the calculation as integers and so even
though you'll get "2.0" you won't ever be able to get "2.2" for
example.
On 2/5/10, Antonio de la Fuente <toni at muybien.org> wrote:
> Hi all,
>
> I'm trying to do exercises from:
>
> http://openbookproject.net/thinkcs/python/english2e/ch05.html
>
> exercise number 3 (slope function) and when I run it:
>
> python ch05.py -v
>
> the doctest for the slope function failed, because is expecting a
> floating point value and not an integer:
>
> Failed example:
> slope(2, 4, 1, 2)
> Expected:
> 2.0
> Got:
> 2
>
> This is the function, and how I modified so it would return a floating
> point value (multiply by 1.0). But this doesn't feel the right way to
> do things, or is it?
>
> def slope(x1, y1, x2, y2):
> """
> >>> slope(5, 3, 4, 2)
> 1.0
> >>> slope(1, 2, 3, 2)
> 0.0
> >>> slope(1, 2, 3, 3)
> 0.5
> >>> slope(2, 4, 1, 2)
> 2.0
> """
> result_slope = ((y2 - y1) / (x2 - x1)) * 1.0
> return result_slope
>
> Another question is, anybody knows if these questions from this online
> book are answered somewhere? I can't manage to find them?
>
> Thank you for your time.
> Antonio.
>
> --
> -----------------------------
> Antonio de la Fuente MartÃnez
> E-mail: toni at muybien.org
> -----------------------------
>
> Guarda que comer y no que hacer.
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
--
Sent from my mobile device
More information about the Tutor
mailing list