-
Notifications
You must be signed in to change notification settings - Fork 0
/
calc.py
72 lines (64 loc) · 1.35 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
58
59
60
61
62
63
64
65
66
67
68
69
70
def calc(text):
ls=[["+", "sum of ","plus"], ["-","difference of", "minus"], ["divided by","divide","/", "by"],["multiplied by", "product of ", "multiply", "into", "x"]]
index=0
indic=0
tt=list(text.split())
for i in tt:
for ind in range(len(ls)):
if i in ls[ind]:
index = ind
indic = 1
break
if indic ==0:
print("INVALID STATEMENT")
if index==0:
ans = fun_add(text)
elif index==1:
ans = fun_sub(text)
elif index==2:
ans = fun_div(text)
elif index==3:
ans = fun_prod(text)
else:
print("INVALID STATEMENT")
return ans
def fun_add(text):
ans=0
for i in text:
try:
ans += int(i)
except:
pass
return ans
def fun_prod(text):
ans=0
for i in text:
try:
ans *= int(i)
except:
pass
return ans
def fun_sub(text):
a=0
b=0
for i in text:
try:
if a==0:
a = int(i)
else:
b = int(i)
except:
pass
return a-b
def fun_div(text):
a=0
b=0
for i in text:
try:
if a==0:
a = int(i)
else:
b = int(i)
except:
pass
return a//b