forked from codemistic/Web-Development
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsmart_calci.py
More file actions
82 lines (70 loc) · 2.03 KB
/
smart_calci.py
File metadata and controls
82 lines (70 loc) · 2.03 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
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
responses=["Welcome to SMART CALCULATOR","My Name is Calci","Thanks","Sorry,this is beyond my ability"]
def extract_numbers_from_text(text):
l=[]
for t in text.split(' '):
try:
l.append(float(t))
except ValueError:
pass
return(l)
def add(a,b):
return a+b
def sub(a,b):
return a-b
def mul(a,b):
return a*b
def div(a,b):
return a/b
def myName():
print(responses[1])
def sorry():
print(responses[3])
def end():
print(responses[2])
input("Press Enter key to exit")
exit()
def help():
print("List of Valid Commands")
for k in operations:
print(k)
for k in commands:
print(k)
#--------------------------------Operations dictionary----------------------------------
operations={
"PLUS":add,"ADD":add,"SUM":add,"ADDITION":add,"+":add,
"MINUS":sub,"SUBTRACT":sub,"SUBTRACTION":sub,"DIFFERENCE":sub,"-":sub,
"MULTIPLY":mul,"PRODUCT":mul,"MULTIPLICATION":mul,"*":mul,
"DIVIDE":div,"DIVISION":div,"/":div
}
#---------------------------------Commands dictionary------------------------------------
commands={
"NAME":myName,
"END":end,
"EXIT":end,
"CLOSE":end,
"EXIT":end,
"HELP":help,
}
#----------------------------------------main()--------------------------------------
def main():
print(responses[0],responses[1],sep='\n')
while True:
print()
text=input("Enter some text: ")
for word in text.split(' '):
if word.upper() in operations.keys():
try:
l=extract_numbers_from_text(text)
r=operations[word.upper()](l[0],l[1])
print(r)
except:
print("Something is wrong,Please retry !!!")
finally:
break
elif word.upper() in commands.keys():
commands[word.upper()]()
break
else:
sorry()
if __name__=="__main__":
main()