简体   繁体   English

为什么我的自定义 function 不能从 Python 中的文件编辑器执行?

[英]Why won't my custom function execute from the file editor in Python?

I recently started a Python course on Udemy.我最近在 Udemy 上开设了 Python 课程。 I couldn't get a grasp on the return function.我无法掌握返回值 function。

For example, in the following simple program:例如,在下面的简单程序中:

def f(a, b):
    x = a + b
    return x

f(3, 3)

Simply executing this as a.py will not display the result 6简单地将其作为 a.py 执行将不会显示结果6

However, manually executing f(3, 3) in the console will display 6但是在控制台手动执行f(3, 3)显示6

Could I get some intuition or explanation as to what is happening here?我能对这里发生的事情有一些直觉或解释吗?

I'm running Python 3.8.3 on Windows 10我在 Windows 10 上运行 Python 3.8.3

You've defined the function in the script, but have not called it anywhere.您已经在脚本中定义了 function,但没有在任何地方调用它。

You need to call it and store the result in a variable.您需要调用它并将结果存储在变量中。

Your script should be like this:你的脚本应该是这样的:

def f(a, b):
    x = a + b
    return x

result = f(3,3)
print(result)

Finally you will run python a.py最后你将运行python a.py

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM