[Tutor] Simple variable type question

Antonio de la Fuente toni at muybien.org
Fri Feb 5 17:54:59 CET 2010


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. 


More information about the Tutor mailing list