Pstie Pstie - 1 year ago 96
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)


Kind regards,

Answer Source

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)

def return_val(a):
    return a

>>> result = return_val(8)
>>> print result
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download