Python Forum
i need help in fixing my scientific calculator coding : (, im using python 3.5.5
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
i need help in fixing my scientific calculator coding : (, im using python 3.5.5
#1
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
from tkinter import*
import math
import parser
import tkinter.messagebox
 
root = Tk()
root.title("La Scientific Calculator")
root.configure(background = "powder blue")
root.resizable(width =False, height=False)
root.geometry("480x568+0+0")
 
calc = Frame(root)
calc.grid()
 
self = "self"
class Calc():
    def __init__(self):
        self.total =0
        self.current = ""
        self.input_value = True
        self.check_sum = False
        self.op = ""
        self.result = False
 
    def numberEnter(self, num):
        self.result = False
        firstnum = txtDisplay.get()
        secondnum = str(num)
        if self.input_value:
            self.current = secondnum
            self.input_value = False
        else:
            if secondnum ==".":
                if secondnum in firstnum:
                    return
                    self.current = firstnum + secondnum
                    self.display(self.current)
 
 
    def sum_of_total(self):
        self.result = True
        self.current = float(self.current)
    if self.check_sum == True:
        self.valid_function()
    else:
         self.total = float(txtDisplay.get())
 
             
    def display(self, value):
        txtDisplay.delete (0, END)
        txtDisplay.insert(0, value)
 
    def valid_fuction(self):
        if self.op == "add":
                self.total += self.current
        if self.op == "sub":
                self.total -= self.current 
        if self.op == "multi":
                self.total *= self.current
        if self.op == "divide":
                self.total /= self.current
        if self.op == "mod":
            self.total %= self.current  
            self.input_value = True
            self.check_sum+ False
            self.display(self.total)
 
 
    def operation(self, op):
        self.current = float(self.current)
        if self.check_sum:
                self.valid_function()
        elif not self.result:
            self.total = self.current
            self.input_value = True
            self.check_sum = True
            self.op = op
            self.result = False
             
    def Clear_Entry(self):
        self.result = False
        self.current ="0"
        self.display(0)
        self.input_value = True
 
    def all_Clear_Entry(self):
        self.Clear.Entry()
        self.total = 0
 
    def mathsPM(self):
        self.result = False
        self.current = -(float(txt.Display.get()))
        self.display(self.current)
 
    def squared(self):
        self.result = False
        self.current = math.sqrt (float(txt.Display.get()))
        self.display(self.current)
 
 
    def cos(self):
        self.result = False
        self.current = math.cos(math.radians(float(txt.Display.get())))
        self.display(self.current)
         
    def cosh(self):
        self.result = False
        self.current = math.cosh(math.radians(float(txt.Display.get())))
        self.display(self.current)
 
         
         
    def tan(self):
        self.result = False
        self.current = math.tan(math.radians(float(txt.Display.get())))
        self.display(self.current)
 
        
    def tanh(self):
        self.result = False
        self.current = math.tanh(math.radians(float(txt.Display.get())))
        self.display(self.current)
  
         
    def sin(self):
        self.result = False
        self.current = math.sinh(math.radians(float(txt.Display.get())))
        self.display(self.current)
 
 
         
 
        def sinh(self):
            self.result = False
            self.current = math.sinh(math.radians(float(txt.Display.get())))
            self.display(self.current)
 
 
        def log(self):
            self.result = False
            self.current = math.log(float(txt.Display.get()))
            self.display(self.current)
 
         
           
        def exp(self):
            self.result = False
            self.current = math.exp(math.radians(float(txt.Display.get())))
            self.display(self.current)
       
        def pi(self):
            self.result = False
            self.current = math.pi
            self.display(self.current)
 
 
        def tau(self):
            self.result = False
            self.current = math.tau
            self.display(self.current)
 
        def e(self):
            self.result = False
            self.current = math.e
            self.display(self.current)
 
        def acosh(self):
            self.result = False
            self.current = math.acosh(float(txtDisplay.get()))
            self.display(self.current)
 
        def asinh(self):
            self.result = False
            self.current = math.asinh(float(txtDisplay.get()))
            self.display(self.current)
 
 
        def expml(self):
            self.result = False
            self.current = math.expml(float(txtDisplay.get()))
            self.display(self.current)
 
        def lgamma(self):
            self.result = False
            self.current = math.lgamma(float(txtDisplay.get()))
            self.display(self.current)
 
        def degrees(self):
            self.result = False
            self.current = math.degree(float(txtDisplay.get()))
            self.display(self.current)
              
        def log2(self):
            self.result = False
            self.current = math.log2(float(txtDisplay.get()))
            self.display(self.current)
   
                      
        def log10(self):
            self.result = False
            self.current = math.log10(float(txtDisplay.get()))
            self.display(self.current)
   
                      
        def log1p(self):
            self.result = False
            self.current = math.log1p(float(txtDisplay.get()))
            self.display(self.current)
   
 
 
 
added_value = calc()
             
 
txtDisplay = Entry(calc, font=("arial",20,"bold"), bg="powder blue", bd=30, width=29, justify=RIGHT)
txtDisplay.grid(row=0, column=0, columnspan=4, pady=1)
txtDisplay.insert(0,"0")
 
numberpad = "789456123"
i =0
btn =[]
for j in range(2,5):
    for k in range(3):
        btn.append(Button(calc, width=6, height=2, font=("arial",20,"bold"), bd=4, text =numberpad[i]))
        btn[i].grid(row =j, column =k, pady=1)
        btn[i]["command"] = lambda x = numberpad [i] : added_value.numberEnter(x)
        i+= 1
#===========================================#==========================#==========================       
btnClear = Button(calc, text=chr(67), width=6, height=2, font=("arial",20,"bold"), bd=4,
                  bg="powder blue", command=added_value.Clear_Entry).grid(row=1,column=0, pady=1)
 
btnALLClear = Button(calc, text=chr(67)+ chr(69),width=6, height=2, font=("arial",20,"bold"), bd=4,
                  bg="powder blue",command=added_value.Clear_Entry).grid(row=1,column=1, pady=1)
 
 
btnSq = Button(calc, text="√", width=6, height=2, font=("arial",20,"bold"), bd=4,
                  bg="powder blue").grid(row=1,column=2, pady=1)
 
btnAdd = Button(calc, text="+", width=6, height=2, font=("arial",20,"bold"), bd=4,
                  bg="powder blue",command = lambda: added_value.operation("add")).grid(row=1,column=3, pady=1)
 
btnSub = Button(calc, text="-", width=6, height=2, font=("arial",20,"bold"), bd=4,
                  bg="powder blue",command = lambda: added_value.numberEnter("sub")).grid(row=2,column=3, pady=1)
 
btnMult = Button(calc, text="*", width=6, height=2, font=("arial",20,"bold"), bd=4,
                  bg="powder blue",command = lambda: added_value.numberEnter("multi")).grid(row=3,column=3, pady=1)
 
btnDiv = Button(calc, text=chr(247), width=6, height=2, font=("arial",20,"bold"), bd=4,
                  bg="powder blue",command = lambda: added_value.numberEnter("divide")).grid(row=4,column=3, pady=1)
 
 
btnZero = Button(calc, text="0", width=6, height=2, font=("arial",20,"bold"), bd=4,
          bg="powder blue",command = lambda: added_value.numberEnter(0)).grid(row=5,column=0, pady=1)
 
 
btnDot = Button(calc, text=".", width=6, height=2, font=("arial",20,"bold"), bd=4,
                  bg="powder blue",command = lambda: added_value.numberEnter(".")).grid(row=5,column=1, pady=1)
 
btnPM = Button(calc, text=chr(177), width=6, height=2, font=("arial",20,"bold"), bd=4,
                  bg="powder blue").grid(row=5,column=2, pady=1)
 
btnEquals = Button(calc, text="=", width=6, height=2, font=("arial",20,"bold"), bd=4,
                  bg="powder blue", command = lambda : added_value.sum_of_total).grid(row=5,column=3, pady=1)
 
#========================Scientific Part======================================================
 
 
btnPi = Button(calc, text="n", width=6, height=2, font=("arial",20,"bold"), bd=4,
         bg="powder blue",command = lambda: added_value.pi).grid(row=1,column=4, pady=1)
 
btnCos= Button(calc, text="cos", width=6, height=2, font=("arial",20,"bold"), bd=4,
                  bg="powder blue").grid(row=1,column=5, pady=1)
 
btntan = Button(calc, text="tan", width=6, height=2, font=("arial",20,"bold"), bd=4,
                  bg="powder blue").grid(row=1,column=6, pady=1)
 
btnsin = Button(calc, text="sin", width=6, height=2, font=("arial",20,"bold"), bd=4,
                  bg="powder blue").grid(row=1,column=7, pady=1)
 
#=====================================================================================================
btn2Pi = Button(calc, text="2n", width=6, height=2, font=("arial",20,"bold"), bd=4,
                  bg="powder blue",command = lambda: added_value.tau).grid(row=2,column=4, pady=1)
 
 
btnCosh = Button(calc, text="cosh", width=6, height=2, font=("arial",20,"bold"), bd=4,
                  bg="powder blue").grid(row=2,column=5, pady=1)
 
btntanh = Button(calc, text="tanh", width=6, height=2, font=("arial",20,"bold"), bd=4,
                  bg="powder blue").grid(row=2,column=6, pady=1)
 
 
btnsinh = Button(calc, text="sinh", width=6, height=2, font=("arial",20,"bold"), bd=4,
                  bg="powder blue").grid(row=2,column=7, pady=1)
 
#===================================================================================================
 
btnlog = Button(calc, text="log", width=6, height=2, font=("arial",20,"bold"), bd=4,
                  bg="powder blue").grid(row=3,column=4, pady=1)
 
btnExp = Button(calc, text="Exp", width=6, height=2, font=("arial",20,"bold"), bd=4,
                  bg="powder blue").grid(row=3,column=5, pady=1)
 
btnMod = Button(calc, text="Mod", width=6, height=2, font=("arial",20,"bold"), bd=4,
                  bg="powder blue").grid(row=3,column=6, pady=1)
 
btnE = Button(calc, text="e", width=6, height=2, font=("arial",20,"bold"), bd=4,
                  bg="powder blue", command = added_value.e).grid(row=3,column=7, pady=1)
 
#=======================================Log Base 2========================================================================
 
 
btnlog2 = Button(calc, text="log2", width=6, height=2, font=("arial",20,"bold"), bd=4,
                  bg="powder blue").grid(row=4,column=4, pady=1)
 
btndeg= Button(calc, text="deg", width=6, height=2, font=("arial",20,"bold"), bd=4,
                  bg="powder blue").grid(row=4,column=5, pady=1)
 
btncosh= Button(calc, text="acosh", width=6, height=2, font=("arial",20,"bold"), bd=4,
                  bg="powder blue").grid(row=4,column=6, pady=1)
 
btnasinh = Button(calc, text="asinh", width=6, height=2, font=("arial",20,"bold"), bd=4,
                  bg="powder blue").grid(row=4,column=7, pady=1)
 
#========================================Log base 10==================================================================
 
btnlog10 = Button(calc, text="log10", width=6, height=2, font=("arial",20,"bold"), bd=4,
                  bg="powder blue").grid(row=5,column=4, pady=1)
 
btnCos = Button(calc, text="log1p", width=6, height=2, font=("arial",20,"bold"), bd=4,
                  bg="powder blue").grid(row=5,column=5, pady=1)
 
btnexpml = Button(calc, text="expml", width=6, height=2, font=("arial",20,"bold"), bd=4,
                  bg="powder blue").grid(row=5,column=6, pady=1)
 
btnlgamma = Button(calc, text="lgamma", width=6, height=2, font=("arial",20,"bold"), bd=4,
                  bg="powder blue").grid(row=5,column=7, pady=1)
 
lblDisplay = Label(calc, text="La Scientific Calculator", font=("arial",40, "bold"), justify=CENTER)
lblDisplay.grid(row = 0, column=4, columnspan =4)
 
#===========================================Menu and functions=============================================
 
def iExit():
        iExit = tkinter.messagebox.askyesno("La Scientific Calulator", "Confirm if you want to exit")
        if iExit > 0:
            root.destroy()
            return
             
def Scientific():
    root.resizable(width=False, height=False)
    root.geometry("944x568+0+0")
 
def Standard():
    root.resizable(width =False, height=False)
    root.geometry("480x568+0+0")
 
 
menubar = Menu(calc)
 
filemenu = Menu(menubar, tearoff =0)
menubar.add_cascade(label = "File", menu=filemenu)
filemenu.add_command(label = "Standard", command = Standard)
filemenu.add_command(label = "Scientific", command = Scientific)
filemenu.add_separator()
filemenu.add_command(label = "Exit", command = iExit)
 
 
editmenu = Menu(menubar, tearoff =0)
menubar.add_cascade(label = "Edit", menu=editmenu)
editmenu.add_command(label = "Cut")
editmenu.add_command(label = "Copy")
editmenu.add_separator()
editmenu.add_command(label = "Paste")
 
helpmenu = Menu(menubar, tearoff=0)
menubar.add_cascade(label = "Help", menu=helpmenu)
helpmenu.add_command(label = "View Help")
 
 
root.config(menu=menubar)
root.mainloop()
Reply
#2
Woah! Two things:

1. Use the python tags to format that script. It makes it much easier to read.
2. What exactly is the problem with the code? How is it misbehaving?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  fixing error TypeError: 'float' object is not subscriptable programmingirl 1 2,433 Jan-28-2023, 08:13 PM
Last Post: deanhystad
  Need help fixing Index Error adesimone 1 2,384 Nov-21-2022, 07:03 PM
Last Post: deanhystad
  Python calculator help but not using while loop with true, any flags variable ,break kirt6405 13 8,343 Jun-08-2021, 06:39 AM
Last Post: Larz60+
  Scientific Calculator Spacecowboy 5 5,245 Dec-04-2019, 09:36 PM
Last Post: Clunk_Head
  Fixing my backup function Beginner_coder123 4 4,049 Jun-15-2019, 07:56 PM
Last Post: Beginner_coder123
  Fixing arrays output in Python WLST script pkbash 2 5,194 Feb-28-2019, 06:20 PM
Last Post: pkbash
  Need help with fixing iteration counter Kapolt 8 6,231 Oct-18-2018, 07:56 PM
Last Post: buran
  python age calculator need to find the number of years before they turn 100 not using orangevalley 4 12,769 Mar-26-2018, 04:44 AM
Last Post: PyMan
  scientific numbers metalray 1 3,232 Feb-06-2018, 06:50 PM
Last Post: boring_accountant

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020