[Tutor] Simple variable type question
Wayne Werner
waynejwerner at gmail.com
Fri Feb 5 19:13:44 CET 2010
On Fri, Feb 5, 2010 at 10:54 AM, 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:
>
try to cast your values as float instead:
try:
x1 = float(x1)
x2 = float(x2)
y1 = float(y1)
y2 = float(y2)
except ValueError:
print "Error, not a floating point!"
raise
Then you don't need to worry about the * 1.0
HTH,
Wayne
>
> 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
>
--
To be considered stupid and to be told so is more painful than being called
gluttonous, mendacious, violent, lascivious, lazy, cowardly: every weakness,
every vice, has found its defenders, its rhetoric, its ennoblement and
exaltation, but stupidity hasn’t. - Primo Levi
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100205/d8765a38/attachment.htm>
More information about the Tutor
mailing list