-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculator_terminal.py
36 lines (26 loc) · 1.07 KB
/
calculator_terminal.py
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
import operator
# Dictionary of arithmetic operators and their respective functions
ops = {'+': operator.add, '-': operator.sub, '*': operator.mul,
'/': operator.truediv, '%': operator.mod, '^': operator.pow}
def calculate():
try:
while True:
first_num = int(input("First Number: "))
user_op = input("Operation: ")
sec_num = int(input("Second Number: "))
# Print the result of user selected operation of (1st number and 2nd number)
print(ops[user_op](first_num, sec_num))
again = input("Would you like to do another calculation? Press any key to continue or 'n' to quit\n")
if again.lower() == 'n':
break
else:
continue
# Extremely basic error handling
except ZeroDivisionError or ValueError:
print("Zero Division Error or Value Error")
except:
print("stop testing me")
if __name__ == '__main__':
print("Calculator Program")
print("Use numerical characters and +, -, *, /, %, ^ operators.")
calculate()