# [Tutor] Simple variable type question

Sander Sweers sander.sweers at gmail.com
Fri Feb 5 19:15:10 CET 2010

```On vr, 2010-02-05 at 16:54 +0000, Antonio de la Fuente wrote:
> 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

Python handles integers a bit counter intuitive. It does not
automatically converts you devision from an int to a float number.

>>> 1 / 2
0
>>> 3 / 2
1

You would expect this to become 0.5 and 1.5.

> 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?

Not is is not, if you would type this into a python shell or idle you
will still fail the 3rd test.

>>> (3-2)/(3-1) * 1.0
0.0

> 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

You will need to find a way to convert your int numbers to float numbers
*before* doing the calculation.

Greets
Sander

```