-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculator.py
136 lines (78 loc) · 2.72 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
"""CLI application for a prefix-notation calculator."""
from arithmetic import (add, subtract, multiply, divide, square, cube,
power, mod, )
def get_input():
"""Prompts user for input in prefix notation"""
input_string = input("Enter your equation: ") # "+ 1 2"
tokens = tokenizer(input_string)
return validate_input(tokens)
def validate_input(tokens):
valid_operators = ['+', '-', '*', '/', 'square', 'cube', 'power', 'mod']
while True:
if tokens[0] == 'q':
print("Thanks for playing!")
return tokens
if tokens[0] not in valid_operators:
print("That's not a valid operator. Please try again.")
input_string = input("Enter your equation: ")
tokens = tokenizer(input_string)
elif tokens[0] == 'square' or tokens[0] == 'cube':
try:
tokens[1] = int(tokens[1])
break
except:
print("You must enter a valid integer.")
input_string = input("Enter your equation again: ")
tokens = tokenizer(input_string)
else:
try:
tokens[1] = int(tokens[1])
tokens[2] = int(tokens[2])
break
except:
print("You must enter two valid integers.")
input_string = input("Enter your equation again: ")
tokens = tokenizer(input_string)
return tokens
def tokenizer(str):
tokens = []
tokens = str.split(" ") # ['+', '1', '2']
return tokens
def do_arithmetic(operator, num1, num2=None):
"""Call function to calculate equation"""
if operator == "+":
return add(num1, num2)
elif operator == "-":
return subtract(num1, num2)
elif operator == "*":
return multiply(num1, num2)
elif operator == "/":
return divide(num1, num2)
elif operator == "square":
return square(num1)
elif operator == "cube":
return cube(num1)
elif operator == "power":
return power(num1, num2)
elif operator == "mod":
return mod(num1, num2)
else:
print("Error")
def calculator():
"""Calculates equation from input"""
while True:
tokens = get_input()
operator = tokens[0]
if operator == 'q':
break
num1 = tokens[1]
if len(tokens) == 3:
num2 = tokens[2]
if len(tokens) == 3:
output = do_arithmetic(operator, num1, num2)
print(output)
else:
output = do_arithmetic(operator, num1)
print(output)
#change
calculator()