/
calculator.py
67 lines (56 loc) · 2.2 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
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
"""CLI application for a prefix-notation calculator."""
from arithmetic import (add, subtract, multiply, divide, square, cube,
power, mod, add_mult, add_cubes)
# Replace this with your code
def translate(tokens):
if tokens[0] == "+":
return add(float(tokens[1]), float(tokens[2]))
elif tokens[0] == "-":
return subtract(float(tokens[1]), float(tokens[2]))
elif tokens[0] == "*":
return multiply(float(tokens[1]), float(tokens[2]))
elif tokens[0] == "/":
return divide(float(tokens[1]), float(tokens[2]))
elif tokens[0] == "square":
return square(float(tokens[1]))
elif tokens[0] == "cube":
return cube(float(tokens[1]))
elif tokens[0] == "pow":
return power(float(tokens[1]), float(tokens[2]))
elif tokens[0] == "mod":
return mod(float(tokens[1]), float(tokens[2]))
elif tokens[0] == "x+":
return add_mult(float(tokens[1]), float(tokens[2]), float(tokens[3]))
elif tokens[0] == "cubes+":
return add_cubes(float(tokens[1]), float(tokens[2]))
else:
return "Please enter a valid operator."
def isValid(tokens):
if tokens[0] not in ["+", "-", "*", "/", "square", "cube", "pow", "mod", "x+", "cubes+"]:
print("Please enter a valid operator.")
elif not floatify(tokens):
print("Your operands are not all numbers. Please try again.")
elif tokens[0] in ["+", "-", "*", "/", "pow", "mod", "cubes+"] and len(tokens) != 3:
print(f"The {tokens[0]} operator requires two operands.")
elif tokens[0] in ["square", "cube"] and len(tokens) != 2:
print(f"The {tokens[0]} operator takes exactly one operand.")
elif tokens[0]=="x+" and len(tokens) != 4:
print("The x+ operator requires three operands.")
else:
return True
return False
def floatify(tokens):
for token in tokens[1:]:
try:
token = float(token)
except ValueError:
return False
return True
while True:
equation = input("Enter your equation > ")
if equation == "q" or equation == "quit":
break
tokens = equation.split(" ")
if not isValid(tokens):
continue
print(translate(tokens))