I am working on a function to find the slope of a line if given x1, y1 and x2, y2. Not too hard. But I need the results to be floating point not integers even though the x and y inputs are integers. I have :

``````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
"""
y = y2 - y1
x = x2 - x1
m = y / x
return m``````

The comment inside the definition is what I am really working on (testing functions), but not my problem right now. It fails the test because it expects 1.0 as the first answer and as an integer gets 1. I tried making m into float(m), but get an error that I can't call a function right there. How do I turn a variable into a floating decimal number?

convert one of them to a float first. Something like: m = float(y) / x

That was the trick! Thanks to all of you guys for helping with such elementary questions.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.