Answer to Question #333912 in Python for Zuu

Question #333912

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)



1
Expert's answer
2022-04-26T09:27:19-0400
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()}")


Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS