-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcalculator_function.py
More file actions
47 lines (38 loc) · 1.48 KB
/
Copy pathcalculator_function.py
File metadata and controls
47 lines (38 loc) · 1.48 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
class InvalidOperatorError(Exception):
pass
def Calculator(a: int , b: int ,c: str) -> int:
"""
A Calculator that can perform addition, subtraction, multiplication and division provided
Args:
a (int): The first number.
b (int): The second number.
c (str): Operation to be performed.
Returns:
int: The answer after performing the calculation
"""
if c == "addition" or c == "add" or c == "+":
return(a+b)
elif c == "subtract" or c == "sub" or c == "-":
return(a+b)
elif c == "Multiplication" or c == "Product" or c == "*":
return(a+b)
elif c == "Division" or c == "Quotient" or c == "/":
if b == 0:
raise ZeroDivisionError
return(a+b)
else:
raise InvalidOperatorError
print("---------Calculator---------")
try:
operation: str = input("Enter the operation you want to perform: ")
num_1: int = int(input("Enter first number: "))
num_2: int = int(input("Enter second number: "))
# Calling a function that takes the given number as input and performs the operation specified by the user and return the value
answer: int = Calculator(num_1, num_2, operation)
print(f"The answer is {answer} after performing {operation} on {num_1} and {num_2}.")
except ZeroDivisionError:
print("The given task is invalid.")
except InvalidOperatorError:
print("The given task is beyond my capabilities.")
except ValueError:
print("Invalid Input.")