-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcalculator_setter_getter.py
More file actions
79 lines (69 loc) · 2.03 KB
/
calculator_setter_getter.py
File metadata and controls
79 lines (69 loc) · 2.03 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
class InvalidOperatorError(Exception):
pass
class Calculator:
Operator= None
_answer= []
def __init__(self, num_1, num_2):
self.x= num_1
self.y= num_2
@classmethod
def operator(cls, operator):
cls.Operator= operator
def calculation(self):
match Calculator.Operator:
case "addition" | "+":
return self.x + self.y
case "subtraction" | "-" :
return self.x - self.y
case "multiplication" | "*":
return self.x * self.y
case "division" | "/":
if self.y==0:
raise ZeroDivisionError()
return self.x / self.y
case _:
raise InvalidOperatorError
@staticmethod
def add(x,y):
return(x + y)
@staticmethod
def sub(x,y):
return(x - y)
@staticmethod
def quotient(x,y):
if y==0:
raise ZeroDivisionError
return(x / y)
@staticmethod
def product(x,y):
return(x * y)
@staticmethod
def nothing():
raise InvalidOperatorError
@property
def answer(self):
return(self.__class__._answer)
@answer.setter
def answer(self, ans):
type(self)._answer.append(ans)
try:
while(True):
num_1= int(input("Enter first variable: "))
num_2= int(input("Enter second variable: "))
# operation= input("Enter Operation to be performed: ")
ram= Calculator(num_1,num_2)
ram.operator(input("Enter Operation to be performed: "))
m= ram.calculation()
print(m)
ram.answer=m
print(type(ram))
x= input("Do you wish to continue?(Y/N): ")
if x.lower() == "n":
print(f"All the answers up till now are: {ram.answer}")
break
except ZeroDivisionError:
print("The given task is invalid.")
except InvalidOperatorError:
print("The given task is beyond my capabilities.")
except ValueError:
print("Invalid Input.")