Pstie Pstie - 1 month ago 8
Python Question

How to return an int value from a function python

I am really new to Python and found this snippet online that I've modified, right now I have it printing x * y but I want to be able to return it as a int value so I can use it again later in the script.

I'm using Python 2.7.6.

def show_xy(event):
xm, ym = event.x, event.y
x3 = xm * ym
print x3
root = tk.Tk()
frame = tk.Frame(root, bg = 'yellow',
width = 300, height = 200)
frame.bind("<Motion>", showxy)
frame.pack()

root.mainloop()


Kind regards,
Postie

Answer

To return a value, you simply use return instead of print:

def showxy(event):
    xm, ym = event.x, event.y
    x3 = xm*ym
    return x3

Simplified example:

def print_val(a):
    print a

>>> print_val(5)
5

def return_val(a):
    return a

>>> result = return_val(8)
>>> print result
8