-
Notifications
You must be signed in to change notification settings - Fork 9
/
class_areas_calculator.py
66 lines (51 loc) · 2.21 KB
/
class_areas_calculator.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
57
58
59
60
61
62
63
64
65
66
# AREA CALCULATOR
# This program asks the user wich area he wants to calculate
# among: circle, square, rectangle and triangle
# Made by Alessandro Silvestri
class Calculator:
def circle(self, radius):
return radius * radius * 3.14
def square(self, side):
return side ** 2
def rectangle(self, side1, side2):
return side1 * side2
def triangle(self, side, height):
return side * height / 2
def area_calculator(self):
# asking the user what area he wants to calculate
user_request = input("What area do you want calculate?: (1:circle 2:square 3:rectangle 4:triangle): ")
if user_request == '1':
a = int(input("insert the circle radius: "))
print(self.circle(a))
self.retry()
elif user_request == '2':
a = int(input("insert the square side: "))
print(self.square(a))
self.retry()
elif user_request == '3':
a = int(input("insert the rectangle side 'a': "))
b = int(input("insert the rectangle side 'b': "))
print(self.rectangle(a, b))
self.retry()
elif user_request == '4':
a = int(input("insert the triangle side: "))
b = int(input("insert the triangle height: "))
print(self.triangle(a, b))
self.retry()
else:
print('wrong command')
self.area_calculator()
def retry(self):
# internal function for asking the user if he wants to use the program again and checks the answer
while True:
retry_quest = input('do you want calculate other areas?: (y/n): ')
if retry_quest.lower() != 'y' and retry_quest.lower() != 'n':
print('wrong command')
else:
break
if retry_quest == 'y':
self.area_calculator()
if retry_quest == 'n':
print('bye!')
area_calculator = Calculator()
area_calculator.area_calculator()