Create a Class Rectangle with the following Attributes and Methods
Attributes: The class has attributes width and length that represent the two sides of a rectangle
Methods:
Add a method named area which returns the area (A) of the rectangle using the formula A=width*length
Add a method named perimeter which returns the perimeter (P) of the rectangle using the formula P=2(length+width)
class Rectangle(object):
def __init__(self, width, length):
self.width = width
self.length = length
def area(self):
A=self.width*self.length
return A
def perimeter(self):
P=2*(self.length+self.width)
return P
if __name__ == "__main__":
w = int(input("Enter width: "))
l = int(input("Enter length: "))
rec = Rectangle(w, l)
print(f"The area of the rectangle: {rec.area()}")
print(f"The perimeter of the rectangle: {rec.perimeter()}")
Comments
Leave a comment