-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculator.py
75 lines (47 loc) · 1.49 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
63
64
65
66
67
68
69
70
71
72
73
74
75
"""CLI application for a prefix-notation calculator."""
from arithmetic import (add, subtract, multiply, divide, square, cube,
power, mod, )
# token = [+, 3, 4]
# Replace this with your code
# if len(tokens) < 3:
# num2 = "0"
# else:
# num2 =float(tokens[2])
# if len(tokens) > 3:
# num3 = float(tokens[3])
def calculator():
operator = tokens[0]
num1 = float(tokens[1])
num2 = float(tokens[2])
try:
if operator == '+':
result = add(num1, num2)
elif operator == '-':
result = subtract(num1, num2)
elif operator == '*':
result = multiply(num1, num2)
elif operator == '/':
result = divide(num1, num2)
elif operator == '**':
result = square(num1)
elif operator == 'pow':
result = power(num1, num2)
elif operator == 'mod':
result = mod(num1,num2)
except IndexError:
if operator == '**':
result = square(num1)
elif operator == 'cube':
result = cube(num1)
return result
while True:
#until 'q' is entered
user_input = input("Enter your equation > ")
tokens = user_input.split(' ')
if user_input == 'q':
break
elif len(tokens) < 2:
print("Error, not enough inputs.")
continue
else:
print(calculator())