-2

UPDATE: I now understand that this was actually just some confusion about scope vs import. I was updating global values and I couldn't find a way of returning more than one value from the other "Sum Function" file. Now, I know that can easily be done using a tuple as pointed out by Alex.

So, this is some code that I wrote. As you can probably see that my math functions don't really take any parameters. And I'm trying to change it as such the Sum, Subtraction and functions like those take two parameters while functions like sin only take one.. Now what i wanted to ask was that how could I achieve giving it parametersn and return the answer instead of updating global values like I've done here. Any and all help will be appreciated

from tkinter import *
import math

root= Tk()
num1=StringVar()


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)


a=0
common=''
condition=0

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: addition() )
addButton.grid(row=7, column=4, ipady=8, ipadx=8, padx=(0, 11))

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

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


def clck (number):
    global common
    common+= str(number)
    num1.set(common)

def sin():
    global common
    global a
    a = math.sin(int(a))
    num1.set(a)

def addition():
    global a
    global common
    try:
        a=a+ int(common)
    except:
        pass
    #print(a)
    num1.set(a)
    common=''
    global condition
    condition='add'
def subtraction():
    global a
    global common
    a=a- int(common)
    #print(a)
    num1.set(a)
    common=''

root.mainloop()

Also, I know right now that I've only implemented a couple buttons. If there's any way that my code could be improved let me know that as well and if there's anything that you don't get.

dydx
  • 147
  • 12
  • But, my program isn't really taking two inputs as parameters.. it's setting the global values as input values and then processing those global values instead of passing them as a parameter into the function.. does that bit of info help? – dydx Feb 12 '18 at 20:45
  • https://docs.python.org/3/tutorial/controlflow.html#defining-functions – wwii Feb 12 '18 at 20:48

1 Answers1

0

Look at this sample code:

def add(arg_1, arg_2, arg_3):
    # do some stuff
    return arg_1 + int(arg_2), "new value", "new condition"

a = 1
common = "old value"
condition = "old condition"    

a, common, condition = sum(a, common, condition)
addButton = Button(root, text="+", width="5", command=lambda: addition(a, common, condition))

If your function takes N variables as arguments and changes each of them, your function can return a tuple of N items which you will be able to "spread" over your variables, thus changing them all. Using this method, you can change global variables without declaring them global in all of the functions.

Apart from that, what does except: offensive_word = 2 mean? You're creating a variable which does nothing. If you want your except block to be empty, just write except: pass.

Nae
  • 10,363
  • 4
  • 30
  • 67
Alex F
  • 86
  • 9
  • Hey! Thanks man! Can you tell me why you're passing "condition" as a parameter? It doesn't make sense, right? Or maybe it does? Also, can you link me to an article that explains this concept: "a, common, condition = sum(a, common, condition)" Or just let me know what it's called and I'll look it up. Thanks!!!!! – dydx Feb 12 '18 at 20:48
  • Actually, I understand what's happening here.. hmm.. interesting – dydx Feb 12 '18 at 20:57
  • I've noticed that you're using `condition` variable in your `addition` function, so I assumed that you need it as well. If your function doesn't need to read or rewrite this variable, than remove it from the function declaration. As for returning multiple value: https://stackoverflow.com/questions/354883/how-do-you-return-multiple-values-in-python. The idea behind `a, b = add(a, b)` is that both (a, b) variables and function's return are tuples so the result is spread over the variables. You can interpret it as `result = add(a, b); a = result[0], b = result[1]`. – Alex F Feb 12 '18 at 21:01
  • Yeah... Sorry about that.. I forgot to replace that with pass... =( – dydx Feb 12 '18 at 21:05