Python 中函数未定义错误
我试图在 python 中定义一个基本函数,但当我运行一个简单的测试程序时,我总是收到以下错误;
>>> pyth_test(1, 2)
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
pyth_test(1, 2)
NameError: name 'pyth_test' is not defined
这是我用于此功能的代码;
def pyth_test (x1, x2):
print x1 + x2
更新:我打开了名为 pyth.py 的脚本,然后当它给出错误时,我在解释器中输入 pyth_test(1,2) 。
感谢您的帮助。 (我为这个基本问题道歉,我以前从未编程过,我正在尝试学习Python作为一种爱好)
import sys
sys.path.append ('/Users/clanc/Documents/Development/')
import test
printline()
## (the function printline in the test.py file
##def printline():
## print "I am working"
I am trying to define a basic function in python but I always get the following error when I run a simple test program;
>>> pyth_test(1, 2)
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
pyth_test(1, 2)
NameError: name 'pyth_test' is not defined
Here is the code I am using for this function;
def pyth_test (x1, x2):
print x1 + x2
UPDATE: I have the script called pyth.py open, and then I am typing in pyth_test(1,2) in the interpreter when it gives the error.
Thanks for the help. (I apologize for the basic question, I've never programmed before and am trying to learn Python as a hobby)
import sys
sys.path.append ('/Users/clanc/Documents/Development/')
import test
printline()
## (the function printline in the test.py file
##def printline():
## print "I am working"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
是的,但是 pyth_test 的定义在哪个文件中声明?它也是在调用之前定位的吗?
编辑:
要全面了解它,请创建一个名为
test.py
的文件,其中包含以下内容:现在运行以下命令:
您应该会看到所需的输出。现在,如果您处于交互式会话中,它应该是这样的:
我希望这解释了声明的工作原理。
为了让您了解布局的工作原理,我们将创建一些文件。使用以下内容创建一个新的空文件夹以保持整洁:
myfunction.py
program.py
现在,如果您run:
它将打印出 3. 现在为了解释出了什么问题,让我们这样修改我们的程序:
现在让我们看看会发生什么:
如前所述,由于上述原因,python 无法找到该模块。因此,您应该将声明放在顶部。
现在,如果我们运行交互式 python 会话:
同样的过程适用。现在,包导入并不是那么简单,所以我建议您了解 模块如何与 Python 一起工作< /a>.我希望这对您有所帮助,并祝您学习顺利!
Yes, but in what file is
pyth_test
's definition declared in? Is it also located before it's called?Edit:
To put it into perspective, create a file called
test.py
with the following contents:Now run the following command:
You should see the output you desire. Now if you are in an interactive session, it should go like this:
I hope this explains how the declaration works.
To give you an idea of how the layout works, we'll create a few files. Create a new empty folder to keep things clean with the following:
myfunction.py
program.py
Now if you run:
It will print out 3. Now to explain what went wrong, let's modify our program this way:
Now let's see what happens:
As noted, python cannot find the module for the reasons outlined above. For that reason, you should keep your declarations at top.
Now then, if we run the interactive python session:
The same process applies. Now, package importing isn't all that simple, so I recommend you look into how modules work with Python. I hope this helps and good luck with your learnings!
它对我有用:
确保在调用函数之前定义该函数。
It works for me:
Make sure you define the function before you call it.
在 python 中,函数不能从任何地方神奇地访问(就像它们在 php 中一样)。必须首先声明它们。所以这会起作用:
但这不会:
In python functions aren't accessible magically from everywhere (like they are in say, php). They have to be declared first. So this will work:
But this won't:
如果您展示用于简单测试程序的代码将会有所帮助。直接放入解释器中这似乎有效。
It would help if you showed the code you are using for the simple test program. Put directly into the interpreter this seems to work.
如果使用 IDLE 安装版本的 Python
if working with IDLE installed version of Python