[Solved] Perimeter Calculation Problem with Python

Perimeter Calculation: Riya, who works as a maths tutor in her free time to teach the orphan kids in her NGO, was now teaching Perimeter calculation to the kids. Being a software engineer she always felt lucky as she could easily write code and display to the kids to give a better way of learning. Before going to the class she starts writing code on perimeter calculation. Let’s help Riya so that she can reach the NGO earlier.

Create an abstract class Shape with an abstract method calculate_perimeter().

Create a class Circle that inherits Shape with following attributes, 

AttributesDatatype
radiusfloat


Create a class Rectangle that inherits  Shape with the following attributes, 

AttributesDatatype
lengthint
breadthint

Create a class Square that inherits Shape with the following attributes, 

AttributesDatatype
sideint

Implement the method calculate_perimeter() in all the child classes to calculate appropriate perimeters.

Note: Use math.pi for pi value and display 2 digits after decimal in the answer.

Print all the output statements in the Main class

Print ‘Invalid Choice’, if the user selects an invalid choice

 Refer to sample input/output for other further details and format of the output.

[All Texts in bold corresponds to the input and rest are output]
Sample Input/Output 1:


List of Shapes:
1.Circle
2.Rectangle
3.Square
Enter your choice:
1
Enter the radius of the Circle:
2.34
The perimeter is 14.70

Sample Input/Output 2:

List of Shapes:
1.Circle
2.Rectangle
3.Square
Enter your choice:
2
Enter the length of the Rectangle:
12
Enter the breadth of the Rectangle:
3
The perimeter is 30.00

Sample Input/Output 3:

List of Shapes:
1.Circle
2.Rectangle
3.Square
Enter your choice:
3
Enter the side of the Square:
13
The perimeter is 52.00

Solution

import math

class Shape:
    def __init__(self):
        pass
    def calculate_perimeter(self):
        pass

class Circle(Shape):
    def __init__(self, radius):
        self.radius = radius
    def calculate_perimeter(self):
        return 2*math.pi*self.radius

class Rectangle(Shape):
    def __init__(self, length, breadth):
        self.length = length
        self.breadth = breadth
    def calculate_perimeter(self):
        return 2*(self.length+self.breadth)

class Square(Shape):
    def __init__(self, side):
        self.side = side
    def calculate_perimeter(self):
        return 4*self.side

# sauravhathi
print("List of Shapes:")
print("1.Circle")
print("2.Rectangle")
print("3.Square")
choice = raw_input("Enter your choice:")
if choice == "1":
    radius = raw_input("\nEnter the radius of the Circle:")
    circle = Circle(float(radius))
    print("\nThe perimeter is {:.2f}".format(circle.calculate_perimeter()))
elif choice == "2":
    length = raw_input("Enter the length of the Rectangle: ")
    breadth = raw_input("Enter the breadth of the Rectangle: ")
    rectangle = Rectangle(int(length), int(breadth))
    print("\nThe perimeter is {:.2f}".format(rectangle.calculate_perimeter()))
elif choice == "3":
    side = raw_input("Enter the side of the Square: ")
    square = Square(int(side))
    print("\nThe perimeter is {:.2f}".format(square.calculate_perimeter()))
else:
    print("\nInvalid Choice")

Happy Learning – If you require any further information, feel free to contact me.

Share your love
Saurav Hathi

Saurav Hathi

I'm currently studying Bachelor of Computer Science at Lovely Professional University in Punjab.

📌 Nodejs and Android 😎
📌 Java

Articles: 444

Leave a Reply

Your email address will not be published. Required fields are marked *