python - 如何从函数python返回一个int值

标签 python python-2.7 int return return-value

我是 Python 的新手,在网上找到了这个我修改过的片段,现在我打印了 x * y 但我希望能够将它作为一个 int 值返回,这样我以后可以再次使用它脚本。

我正在使用 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()

亲切的问候, postman

最佳答案

要返回一个值,您只需使用 return 而不是 print:

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

简化示例:

def print_val(a):
    print a

>>> print_val(5)
5

def return_val(a):
    return a

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

关于python - 如何从函数python返回一个int值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23580244/

相关文章:

python - 将数字的数字写入数组

java - 如何在java中添加自己的数字范围

python - 在colab上-class_weight导致ValueError : The truth value of an array with more than one element is ambiguous.使用a.any()或a.all()

linux - 如何安装pythonwifi库?

python - 在Python中的多重继承中使用相同的函数

java - 获取 "?"的 ASCII 值

java - 将整数添加到Java中的数组

python - __getattr__中调用__getattribute__会无限大吗?

python - python 中的 grep/zgrep 使用子进程

python - 与`sys.getsizeof`不一致