generated from microsoft/vscode-remote-try-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshapes.py
56 lines (42 loc) · 1.18 KB
/
shapes.py
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
from math import pi, sqrt
class Shape():
def __init__(self):
self.area_value = 0
def calculate_area(self):
self.area_value = self.area()
return self.area_value
def area(self):
raise NotImplementedError("Not implemented!")
class Square(Shape):
def __init__(self, side):
super().__init__()
self.side = side
def area(self):
return round(self.side ** 2, 1)
class Circle(Shape):
def __init__(self, radius):
super().__init__()
self.radius = radius
def area(self):
return round(pi * self.radius ** 2, 1)
class Triangle(Shape):
def __init__(self, base, height):
super().__init__()
self.base = base
self.height = height
def area(self):
return round(0.5 * self.base * self.height, 1)
class Hexagon(Shape):
def __init__(self, side):
super().__init__()
self.side = side
def area(self):
return round((3 * sqrt(3) * self.side ** 2) / 2, 1)
shapes = [
Square(4),
Circle(3),
Triangle(4, 5),
Hexagon(2)
]
for shape in shapes:
print(f"The area of the {shape.__class__.__name__} is {shape.calculate_area()}")