-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWordCalc.py
More file actions
56 lines (51 loc) · 1.29 KB
/
WordCalc.py
File metadata and controls
56 lines (51 loc) · 1.29 KB
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
DIGITS = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]
OPS = {"plus": "+",
"minus":"-",
"times":"*",
"/":"/"}
def printError(flag):
if flag == 0 or flag == 2:
print ("Your %s value is incorrect" % ("second" if flag == 2 else "first"))
elif flag == 1:
print ("Your operation is incorrect")
else:
print ("Please enter the whole operation")
"""
def main():
while True:
op = ""
express = raw_input("Enter Calculation: ")
express = express.split()
if (len(express) == 3):
if (express[0] in DIGITS):
op += str(DIGITS.index(express[0]))
if(express[1] in OPS):
op += OPS[express[1]]
if (express[2] in DIGITS):
if (DIGITS.index(express[2]) == 0 and express[1] =="/"):
print ("Can't divide by zero")
continue
else:
op += str(DIGITS.index(express[2]))
break
else: printError(2)
else: printError(1)
else: printError(0)
else: printError(-1)
print eval(op)
main()
"""
def main():
while True:
op = ""
exp = raw_input("Enter calculation: ")
exp = exp.split(" ")
if (exp[0] in DIGITS and exp[2] in DIGITS):
exp[0] = str(DIGITS.index(exp[0]))
exp[2] = str(DIGITS.index(exp[2]))
if (exp[1] in OPS):
exp[1] = OPS[exp[1]]
op = "".join(exp)s
print eval(op)
main()
main()