forked from fenyx-it-academy/Class4-PythonModule-Week6
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patha1_w6.py
More file actions
140 lines (123 loc) · 5.08 KB
/
a1_w6.py
File metadata and controls
140 lines (123 loc) · 5.08 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
'''
#############################################################################################
#*******************************************************************************************#
# Copyright (c) 2020 pyCoder|semih Corporation;) All rights reserved. ##
# [Timestamp:20210216] ##
#*******************************************************************************************#
#############################################################################################
'''
# Create a class named Triangle and Rectangle.
# Create a subclass named Square inherited from Rectangle.
# Create a subclass named Cube inherited from Square.
# Create a subclass named Pyramid multiple inherited both from Triangle and Square.
# Two dimensional classes (Triangle, Rectangle and Square) should have:
# its dimensions as attributes.(can be inherited from a superclass)
# methods which calculate its area and perimeter separately. Three dimensional classes (Cube and Pyramid) should have:
# its dimensions as attributes which are inherited from a superclass
# its extra dimensions if there is. (hint: maybe for Pyramid)
# methods which calculate its volume and surface area separately. (surface area is optional, you may not do this)
menu = ["+===============================+",
"|*|*|*| SHAPE FACTORY |*|*|*|",
"+===============================+",
"\t\tTriangle --> 1",
"\t\tRectangle --> 2",
"\t\tSquare ---> 3",
"\t\tCube -----> 4",
"\t\tPyramid --> 5",
"+===============================+"]
for i in menu:
print ('\t'.expandtabs(44), i)
class Triangle:
def __init__(self, edge1=0, edge2=0, edge3=0):
while True:
try:
self.edge1 = int(input("Enter edge1:"))
self.edge2 = int(input("Enter edge2:"))
self.edge3 = int(input("Enter edge3:"))
break
except:
print("enter a number please!")
def area(self):
hp = (self.edge1+self.edge2+self.edge3) / 2
ta = (hp*(hp-self.edge1)*(hp-self.edge2)*(hp-self.edge3))**0.5
return ta
def perimeter(self):
return self.edge1+self.edge2+self.edge3
def __str__(self):
print(f"Triangle >>> AREA= {self.area():.1f} PERIMETER= {self.perimeter()}")
class Rectangle:
def __init__(self, edge1=0, edge2=0):
while True:
try:
if user_choice==3 or user_choice==4:
self.edge1 = int(input("Enter edge1:"))
break
else:
self.edge1 = int(input("Enter edge1:"))
self.edge2 = int(input("Enter edge2:"))
break
except:
print("Enter a number please!")
def area(self):
if user_choice==3 or user_choice==4:
return self.edge1 * self.edge1
else:
return self.edge1 * self.edge2
def perimeter(self):
if user_choice==3 or user_choice==4:
return self.edge1 * 4
else:
return (self.edge1 + self.edge2)*2
def __str__(self):
print("Rectangle >>> AREA= {} PERIMETER= {}".format(self.area(),self.perimeter()))
class Square(Rectangle):
def __init__(self, edge1=0,edge2=None):
super().__init__(edge1, edge2)
def __str__(self):
print("Square >>> AREA= {} PERIMETER= {}".format(self.area(),self.perimeter()))
class Cube(Square):
def __init__(self,edge1=0):
super().__init__(edge1)
def volume(self):
return self.edge1**3
def __str__(self):
print("Cube >>> SURFACE AREA= {} VOLUME= {}".format(6*self.area(),self.volume()))
class Pyramid(Triangle, Rectangle):
def __init__(self,edge1=0,edge2=0,edge3=0):
super().__init__(edge1,edge2,edge3)
def volume(self):
return (self.edge1*self.edge2*self.edge3)*1/3
def __str__(self):
print("Pyramid >>> VOLUME= {0:.1f}".format(self.volume()))
while True:
user_choice = input("Your choice: ")
try:
user_choice = int(user_choice)
except:
print("Incorrect entry. Please try again!")
if user_choice == 1:
print("For Triangle >>> Enter 3 edge lengths:")
t1 = Triangle()
t1.__str__()
elif user_choice == 2:
print("For Rectangle >>> Enter 2 edge lengths:")
r1 = Rectangle()
r1.__str__()
elif user_choice == 3:
print("For Square >>> Enter only 1 edge lengths:")
s1 = Square()
s1.__str__()
elif user_choice == 4:
print("For Cube >>> Enter only 1 edge lengths:")
c1 = Cube()
c1.__str__()
elif user_choice == 5:
print("For Pyramid >>> Enter 3 edge lengths:")
p1 = Pyramid()
p1.__str__()
q = input("Do you want to continue (Y/N):")
if q.upper() == 'Y':
continue
else:
print("Have nice day!..")
break