-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalc.py
57 lines (50 loc) · 1.41 KB
/
calc.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
import re
#Example: number.number + number.number
# 0.1 + 0.2 = 0.30000000000000004
parts = re.compile(r'([0-9.]*)([+*-/][0-9.]*)')
def badParser(user_input):
# Remove spaces from user_input
user_input = user_input.replace(" ", "")
state = False
# Regular expression... sorry
for (group1, change) in re.findall(parts, user_input):
if(state == False):
# Set Initial number
state = getNumber(group1)
state = parseChange(state, change)
return getNumber(state)
def parseChange(state, change):
# action can be + - * /
action = change[0]
# remove action from change
number2 = getNumber(change[1:])
if(action == "*"):
return state * number2
if(action == "+"):
return state + number2
if(action == "/"):
if(number2 == 0):
print("ERROR: Cannot divide by 0")
return state
return state / number2
if(action == "-"):
return state - number2
def getNumber(string):
#Float or int (Its a mess)
if "." in str(string):
try:
return float(string)
except ValueError:
return 0
else:
try:
return int(string)
except ValueError:
return 0
def header():
print("A Calculator\nActions: * + - /")
header()
while True:
user_input = input("Question: ")
answer = badParser(user_input)
print(answer)