I have a Rectangle class that finds the area of a Rectangle using top-left and bottom-right coordinates. I'm having trouble writing up a test function to test the code and verify it works.
class Rectangle: # rectangle class
# make rectangle using top left and bottom right coordinates
def __init__(self,tl,br):
self.tl=tl
self.br=br
self.width=abs(tl.x-br.x) # width
self.height=abs(tl.y-br.y) # height
def area(self):
return self.width*self.height
So far I have written this which leads to an AttributeError: 'tuple' object has no attribute 'x'
def test_rectangle():
print("Testing rectangle class")
rect = Rectangle((3,10),(4,8))
actual = rect.area()
print("Result is %d" % actual)
What can I change to the code in order to make it work?
Aucun commentaire:
Enregistrer un commentaire