Handle chained calculations properly

python

(dy dx) #1

So, this is a really tricky issue… I know this may be a code dump but you have to check out the full code for this problem to really set in. As you can probably see: my code is trying to implement chained calculations. Like 2+2+3-2 and the final answer becomes 5 regardless of any operation button pressed after this execution so like: 2+2+3-2 and then if I press minus or plus, it shouldn’t matter, it should just give me the answer and get the answer ready for the operation pressed afterwards). So, that’s my thought process. And here’s the code:

#<------------------------------------------------------------IMPORT STATEMENTS----------------------------------------------->
from tkinter import *
import add
import subtract

#<------------------------------------------------------------SETTING GLOBALS------------------------------------------------->
root= Tk()
num1=StringVar()
ans = 0
common = ''
condition = ''

#<------------------------------------------------------------DECIDER---------------------------------------------------------->
def Decide(key):
    decider = {'+': SetAdd, '-': SetSubtract}
    global condition
    if condition == '':
        decider[key]()
    else:
        key = condition
        decider[key]()
        #condition = ''


#<-------------------------------------------------------------SETTERS--------------------------------------------------------->
def SetAdd():
    global ans, common, condition
    ans, common, condition = add.Add(ans, common, condition)
    num1.set(ans)
    print(ans)

def SetSubtract():
    global ans, common, condition
    ans, common, condition = subtract.Subtract(ans, common, condition)
    num1.set(ans)
    print(ans)

#<-------------------------------------------------------------ENTRY BOX-------------------------------------------------------->
txtDisplay = Entry(root, textvariable = num1, width=17, font='Arial 25',justify="right");
txtDisplay.focus();
txtDisplay.grid(columnspan=5,row=0,ipady=8,padx=18,pady=10)

#<-------------------------------------------------------------BUTTONS---------------------------------------------------------->
oneButton = Button(root, text="1", width='5', command=lambda: clck(1))
oneButton.grid(row=6, column=1, ipady=8, ipadx=8)
twoButton = Button(root, text="2", width='5',command = lambda: clck(2))
twoButton.grid(row=6, column=2, ipady=8, ipadx=8)

addButton = Button(root, text="+", width='5',command = lambda: Decide('+'))
addButton.grid(row=7, column=4, ipady=8, ipadx=8, padx=(0, 11))

subButton = Button(root, text="-", width='5',command = lambda: Decide('-'))
subButton.grid(row=8, column=4, ipady=8, ipadx=8, padx=(0, 11))

#<-------------------------------------------------------------CLICKER/INPUT SETTER---------------------------------------------->
def clck (number):
    global ans
    global common
    common+= str(number)
    num1.set(common)

#<-------------------------------------------------------------MAINLOOP---------------------------------------------------------->
root.mainloop()

Here’s the add module:

def Add(param1, param2, param3):
    try:
        param1+=int(param2)
    except:
        pass
    return param1, '', '+'

Here’s the subtract module:

def Subtract(param1, param2, param3):
    try:
        if param1 == 0:
            param1-=int(param2)
            param1*=-1
        else:
            param1-=int(param2)
    except:
        pass
    return param1, '', '-'

So, what happens is that when I press 2+2-2- in that series, it starts to add even when i press the add button. I know why it’s doing that it’s because since the interpreter never jumps to the subtract module so it never changes the condition… I hope I was clear enough. Please get me a way to do chain operations correctly. It’d be a great help! =)