/
calculator.py
35 lines (32 loc) · 1.16 KB
/
calculator.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
"""CLI application for a prefix-notation calculator."""
from arithmetic import (add, subtract, multiply, divide, square, cube,
power, mod, )
def calculator():
# Replace this with your code
# #repeat forever:
while True:
# read input (e.g. + 1 2)
nums_input = input("Enter your equation: ")
# tokenize input
tokens = nums_input.split(' ')
# if the first token is "q":
if tokens[0] == "q":
#quit
break
elif tokens[0] == "+":
print(add(int(tokens[1]), int(tokens[2])))
elif tokens[0] == "-":
print(subtract(int(tokens[1]), int(tokens[2])))
elif tokens[0] == "*":
print(multiply(int(tokens[1]), int(tokens[2])))
elif tokens[0] == "/":
print(divide(int(tokens[1]), int(tokens[2])))
elif tokens[0] == "square":
print(square(int(tokens[1])))
elif tokens[0] == "cube":
print(cube(int(tokens[1])))
elif tokens[0] == "pow":
print(power(int(tokens[1]), int(tokens[2])))
elif tokens[0] == "mod":
print(mod(int(tokens[1]), int(tokens[2])))
calculator()