-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcalculator_function_decorator.py
More file actions
43 lines (35 loc) · 1.23 KB
/
calculator_function_decorator.py
File metadata and controls
43 lines (35 loc) · 1.23 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
class InvalidOperationError(Exception):
pass
def decorator(func):
def wrapper(*args,**kwargs):
print("Initializing Decorator...")
answer: int =func(*args,**kwargs)
print("Operation Successful...")
return answer
return wrapper
def calculator(x,y,a):
match a:
case "addition" | "+":
return x+y
case "subtraction" | "-" :
return x-y
case "multiplication" | "*":
return x*y
case "division" | "/":
if y==0:
raise ZeroDivisionError()
return x/y
case _:
raise InvalidOperationError
try:
num_1= int(input("Enter first number: "))
num_2= int(input("Enter second number: "))
operation= input("Enter the operation to be performed: ")
dec= decorator(calculator)
print(f"The answer is {dec(num_1,num_2,operation)} after performing {operation} in {num_1} and {num_2}.")
except ZeroDivisionError:
print("The given task is invalid.")
except InvalidOperationError:
print("The given task is beyond my capabilities.")
except ValueError:
print("Invalid Input.")