Python 中函数未定义错误

发布于 2024-11-07 06:55:25 字数 696 浏览 0 评论 0原文

我试图在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

只是在用心讲痛 2024-11-14 06:55:25

是的,但是 pyth_test 的定义在哪个文件中声明?它也是在调用之前定位的吗?

编辑:

要全面了解它,请创建一个名为 test.py 的文件,其中包含以下内容:

def pyth_test (x1, x2):
    print x1 + x2

pyth_test(1,2)

现在运行以下命令:

python test.py

您应该会看到所需的输出。现在,如果您处于交互式会话中,它应该是这样的:

>>> def pyth_test (x1, x2):
...     print x1 + x2
... 
>>> pyth_test(1,2)
3
>>> 

我希望这解释了声明的工作原理。


为了让您了解布局的工作原理,我们将创建一些文件。使用以下内容创建一个新的空文件夹以保持整洁:

myfunction.py

def pyth_test (x1, x2):
    print x1 + x2 

program.py

#!/usr/bin/python

# Our function is pulled in here
from myfunction import pyth_test

pyth_test(1,2)

现在,如果您run:

python program.py

它将打印出 3. 现在为了解释出了什么问题,让我们这样修改我们的程序:

# Python: Huh? where's pyth_test?
# You say it's down there, but I haven't gotten there yet!
pyth_test(1,2)

# Our function is pulled in here
from myfunction import pyth_test

现在让我们看看会发生什么:

$ python program.py 
Traceback (most recent call last):
  File "program.py", line 3, in <module>
    pyth_test(1,2)
NameError: name 'pyth_test' is not defined

如前所述,由于上述原因,python 无法找到该模块。因此,您应该将声明放在顶部。

现在,如果我们运行交互式 python 会话:

>>> from myfunction import pyth_test
>>> pyth_test(1,2)
3

同样的过程适用。现在,包导入并不是那么简单,所以我建议您了解 模块如何与 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:

def pyth_test (x1, x2):
    print x1 + x2

pyth_test(1,2)

Now run the following command:

python test.py

You should see the output you desire. Now if you are in an interactive session, it should go like this:

>>> def pyth_test (x1, x2):
...     print x1 + x2
... 
>>> pyth_test(1,2)
3
>>> 

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

def pyth_test (x1, x2):
    print x1 + x2 

program.py

#!/usr/bin/python

# Our function is pulled in here
from myfunction import pyth_test

pyth_test(1,2)

Now if you run:

python program.py

It will print out 3. Now to explain what went wrong, let's modify our program this way:

# Python: Huh? where's pyth_test?
# You say it's down there, but I haven't gotten there yet!
pyth_test(1,2)

# Our function is pulled in here
from myfunction import pyth_test

Now let's see what happens:

$ python program.py 
Traceback (most recent call last):
  File "program.py", line 3, in <module>
    pyth_test(1,2)
NameError: name 'pyth_test' is not defined

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:

>>> from myfunction import pyth_test
>>> pyth_test(1,2)
3

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!

兔小萌 2024-11-14 06:55:25

它对我有用:

>>> def pyth_test (x1, x2):
...     print x1 + x2
...
>>> pyth_test(1,2)
3

确保在调用函数之前定义该函数。

It works for me:

>>> def pyth_test (x1, x2):
...     print x1 + x2
...
>>> pyth_test(1,2)
3

Make sure you define the function before you call it.

画骨成沙 2024-11-14 06:55:25

在 python 中,函数不能从任何地方神奇地访问(就像它们在 php 中一样)。必须首先声明它们。所以这会起作用:

def pyth_test (x1, x2):
    print x1 + x2

pyth_test(1, 2)

但这不会:

pyth_test(1, 2)

def pyth_test (x1, x2):
    print x1 + x2

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:

def pyth_test (x1, x2):
    print x1 + x2

pyth_test(1, 2)

But this won't:

pyth_test(1, 2)

def pyth_test (x1, x2):
    print x1 + x2
世界和平 2024-11-14 06:55:25

如果您展示用于简单测试程序的代码将会有所帮助。直接放入解释器中这似乎有效。

>>> def pyth_test (x1, x2):
...     print x1 + x2
... 
>>> pyth_test(1, 2)
3
>>> 

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.

>>> def pyth_test (x1, x2):
...     print x1 + x2
... 
>>> pyth_test(1, 2)
3
>>> 
裂开嘴轻声笑有多痛 2024-11-14 06:55:25

如果使用 IDLE 安装版本的 Python

>>>def any(a,b):
...    print(a+b)
...
>>>any(1,2)
3

if working with IDLE installed version of Python

>>>def any(a,b):
...    print(a+b)
...
>>>any(1,2)
3
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文