-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
103 lines (86 loc) · 3.39 KB
/
main.py
File metadata and controls
103 lines (86 loc) · 3.39 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
"""
WAP a program in python ot implement a calculator that operator using concepts of
OOP
Decorators: A function that takes another function as an argument and returns a new function.
Setters and Getters
Static and Class Method
Magic Dunder
Exception Handling
Map, Filter and Reduce
Walrus Operators
args and Kwargs
File Handling
OS and Shutil Modules
Command Line Utilities
"""
import time
def decorator(func):
def wrapper(*args, **kwargs):
print("Initiating the operation...")
result = func(*args, **kwargs)
return result
print("Initiating the session...")
return wrapper
class InvalidOperationError(Exception):
"""Raised when an invalid operation is specified."""
pass
@decorator
def calculator(x: int ,y: int ,a: str )-> int | None :
"""
Performs a mathematical operation on two integers based on the specified operator.
Args:
x (int): The first number.
y (int): The second number.
a (str): The operation to perform. Must be one of 'addition', 'subtraction',
'multiplication', 'division', or their symbols ('+', '-', '*', '/').
Returns:
int | None: The result of the operation, or None if an error occurs.
Raises:
ZeroDivisionError: If division is attempted with y = 0.
InvalidOperationError: If an invalid operation is specified.
"""
try:
# Map operation strings to mathematical operations
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
except ZeroDivisionError:
print(f"The denominator cannot be 0 for division operation.")
except InvalidOperationError:
print("The operation is not possible with my current capabilities.")
except Exception as e:
print(f"The program did not execute because {e}.")
if __name__ == "__main__":
start_measure_time = time.time()
results: list[int | str] = []
# Continuously prompt user for inputs until they choose to exit
while True:
try:
x: int = int(input("Enter first number: "))
y: int = int(input("Enter second number: "))
a: str = input("Enter what operation to perform(addition, subtraction, division, multiplication): ")
# print(f"The value of {x} and {y} upon performing {a} is {"Invalid Attempt" if(t:=calci(x, y, a)) is None else t}.")
print(f"Performing {a} on {x} and {y} is an {(t:= 'Invalid Attempt' if (t := calculator(x, y, a)) is None else t)}.")
results.append(t)
except Exception as e:
print(f"The execution did not work because {e}.")
s: str = input("do you want to continue (Y/N): ")
if s.upper() != "Y":
print(f"All results from operations: {results}.")
print("Terminating the session...")
print("Thank you for choosing us.")
end_measure_time = time.time()
print(f"The time taken by the program is: {end_measure_time - start_measure_time:.4f} seconds.")
break
if __name__ != "__main__":
calculate= decorator(calculator)