-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcalculator_oops.py
More file actions
62 lines (53 loc) · 1.45 KB
/
calculator_oops.py
File metadata and controls
62 lines (53 loc) · 1.45 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
class InvalidOperatorError(Exception):
pass
class Calculator:
Operator= None
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, Op):
match Op:
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
try:
ram= Calculator(23,32)
print(Calculator.Operator)
ram.operator("division")
print(Calculator.Operator)
print(ram.calculation("addition"))
except ZeroDivisionError:
print("The given task is invalid.")
except InvalidOperatorError:
print("The given task is beyond my capabilities.")
except ValueError:
print("Invalid Input.")