-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path12. Q4.py
More file actions
61 lines (49 loc) · 1.83 KB
/
12. Q4.py
File metadata and controls
61 lines (49 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import math
class Shape:
def __init__(self):
self.shape = None
def input_data(self):
self.shape = input("Enter shape (square/rectangle/circle/triangle): ").lower()
if self.shape == "square":
self.side = float(input("Enter side length of the square: "))
elif self.shape == "rectangle":
self.length = float(input("Enter length of the rectangle: "))
self.breadth = float(input("Enter breadth of the rectangle: "))
elif self.shape == "circle":
self.radius = float(input("Enter radius of the circle: "))
elif self.shape == "triangle":
self.side = float(input("Enter side length of the equilateral triangle: "))
else:
print("Unsupported shape")
def perimeter(self):
if self.shape == "square":
return 4 * self.side
elif self.shape == "rectangle":
return 2 * (self.length + self.breadth)
elif self.shape == "circle":
return 2 * math.pi * self.radius
elif self.shape == "triangle":
return 3 * self.side
else:
return None
def area(self):
if self.shape == "square":
return self.side ** 2
elif self.shape == "rectangle":
return self.length * self.breadth
elif self.shape == "circle":
return math.pi * self.radius ** 2
elif self.shape == "triangle":
return (math.sqrt(3) / 4) * self.side ** 2
else:
return None
# Example usage
shape = Shape()
shape.input_data()
p = shape.perimeter()
a = shape.area()
if p is not None and a is not None:
print(f"Perimeter/Circumference = {p:.2f}")
print(f"Area = {a:.2f}")
else:
print("Calculation could not be performed.")