I ran the following code in Python 2.7 and got an error. Why?
CODE:
def triangle_area(a, b, c):
"""
Returns the area of a triangle given the length of three sides
Code source: [here][1]
"""
def distance(p1, p2):
return math.hypot(p1[0]-p2[0], p1[1]-p2[1])
side_a = distance(a, b)
side_b = distance(b, c)
side_c = distance(c, a)
s = 0.5 * ( side_a + side_b + side_c)
return math.sqrt(s * (s - side_a) * (s - side_b) * (s - side_c))
Running the following: y = triangle_area(10.1,1.1,11.2)
Produces this error: Traceback (most recent call last):
[snip]
....in distance
return math.hypot(p1[0]-p2[0], p1[1]-p2[1])
TypeError: 'float' object has no attribute '__getitem__'
P1orP2is float objects.It has no indexing.distancefunction, which then tries to index them. You can't do that. What are you expecting it to do?