-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinheritance.py
More file actions
58 lines (49 loc) · 1.4 KB
/
Copy pathinheritance.py
File metadata and controls
58 lines (49 loc) · 1.4 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
import math
# Base class
class Shape:
def area(self):
return 0
# Derived class for Rectangle
class Rectangle(Shape):
def __init__(self, width, height):
self.width = width
self.height = height
def area(self):
return self.width * self.height
# Derived class for Circle
class Circle(Shape):
def __init__(self, radius):
self.radius = radius
def area(self):
return math.pi * self.radius ** 2
# Main program
if __name__ == "__main__":
print("Choose a shape:")
print("1. Rectangle")
print("2. Circle")
choice = input("Enter choice (1/2): ")
if choice == "1":
width = float(input("Enter the width of the rectangle: "))
height = float(input("Enter the height of the rectangle: "))
rect = Rectangle(width, height)
print(f"Area of Rectangle: {rect.area()}")
elif choice == "2":
radius = float(input("Enter the radius of the circle: "))
circ = Circle(radius)
print(f"Area of Circle: {circ.area():.2f}")
else:
print("Invalid choice!")
OUTPUT:
Choose a shape:
1. Rectangle
2. Circle
Enter choice (1/2): 1
Enter the width of the rectangle: 2
Enter the height of the rectangle: 3
Area of Rectangle: 6.0
Choose a shape:
1. Rectangle
2. Circle
Enter choice (1/2): 2
Enter the radius of the circle: 3
Area of Circle: 28.27