forked from CodeMouse92/DeadSimplePython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalculator.py
More file actions
50 lines (44 loc) · 1.19 KB
/
calculator.py
File metadata and controls
50 lines (44 loc) · 1.19 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
import logging
from operator import add, sub, mul, truediv
import sys
logging.basicConfig(filename='log.txt', level=logging.INFO)
def calculator(a, b, op):
a = float(a)
b = float(b)
if op == '+':
return add(a, b)
elif op == '-':
return sub(a, b)
elif op == '*':
return mul(a, b)
elif op == '/':
return truediv(a, b)
else:
raise NotImplementedError(f"No operator {op}")
print("""CALCULATOR
Use postfix notation.
Ctrl+C or Ctrl+D to quit.
""")
while True:
try:
equation = input(" ").split()
result = calculator(*equation)
print(result)
except NotImplementedError as e:
print("<!> Invalid operator.")
logging.info(e)
except ValueError as e:
print("<!> Expected format: <A> <B> <OP>")
logging.info(e)
except TypeError as e:
print("<!> Wrong number of arguments. Use: <A> <B> <OP>")
logging.info(e)
except ZeroDivisionError as e:
print("<!> Cannot divide by zero.")
logging.info(e)
except (KeyboardInterrupt, EOFError):
print("\nGoodbye.")
sys.exit(0)
except Exception as e:
logging.exception(e)
raise