-
Notifications
You must be signed in to change notification settings - Fork 6
/
smart_calc.py
99 lines (74 loc) · 2.28 KB
/
smart_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
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
from tkinter import *
#MATH
def add(a,b):
return a + b
def sub(a,b):
return a - b
def mul(a, b):
try:
return a * b
except ZeroDivisionError:
return "Can't divide with zero"
def div(a, b):
return a/b
def mod(a, b):
return a % b
def lcm(a, b):
L = a if a>b else b
while L <=a*b:
if L %a == 0 and L % b == 0:
return L
L+=1
def hcf(a, b):
H = a if a<b else b
while H >=1:
if a % H == 0 and b % H==0:
return H
H -=1
#CAlculate
def extract_from_text(text):
l = []
for t in text.split(' '):
try:
l.append(float(t))
except ValueError:
pass
return l
def calculate():
text = textin.get()
for word in text.split(' '):
if word.upper() in operations.keys():
try:
l = extract_from_text(text)
r = operations[word.upper()](l[0],l[1])
lst.delete(0,END)
lst.insert(END,"The answer is {}".format(r))
except:
lst.delete(0,END)
lst.insert(END,"SOMETHING WENT WRONG!\nPlease Enter Again")
finally:
break
elif word.upper() not in operations.keys():
lst.delete(0,END)
lst.insert(END,"OOPS!Seems like a new function that I didn't learned yet")
#OPERATIONS
operations = {"ADD":add, "ADDITION":add,"SUM":add,"PLUS": add, "SUB":sub,"DIFFERENCE": sub, "MINUS":sub,"SUBTRACT":sub,"LCM":lcm,"HCF":hcf,"PRODUCT":mul,"MULTIPLY":mul,"MUL":mul,"INTO":mul,"MULTIPLICATION":mul,"DIVISION":div,"DIVIDE":div,"DIV":div,"MOD":mod,"REMAINDER":mod,"MODULUS":mod}
#FRONTEND
win = Tk()
win.geometry('500x300')
win.title("MLBACKBENCHERS")
win.configure(bg = "yellow")
l1 = Label(win, text="Mlbackbencher Smart Calc.", width =25,padx = 3)
l1.place(x=150, y = 10)
l2 = Label(win, text="I am MLbot", width =20,padx = 3)
l2.place(x=170, y = 40)
l3 = Label(win, text="How can I help you?", width =25,padx = 3)
l3.place(x=150, y = 100)
textin = StringVar()
e1 = Entry(win, width= 30, textvariable = textin)
e1.place(x= 130,y=130)
b1 = Button(win, text="Just This", command = calculate)
b1.place(x=210, y = 160)
lst = Listbox(win, width = 30,height = 3)
lst.place(x = 130, y= 200)
win.mainloop()