函数在Python中没有定义错误 [英] function is not defined error in Python

查看:230
本文介绍了函数在Python中没有定义错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在python中定义一个基本函数,但是当我运行一个简单的测试程序时,我总是得到以下错误;

 >>> pyth_test(1,2)

Traceback(最近一次调用的最后一个):
在< module>中,第1行的文件< pyshell#2>
pyth_test(1,2)
NameError:名称'pyth_test'未定义

这里是我用于这个函数的代码;

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

更新:我已经打开了pyth.py脚本,然后输入pyth_test(1,2)在解释器给出错误时。



感谢您的帮助。 (我为基本问题表示歉意,我从未编程过,并且正在努力学习Python作为一项业余爱好)






 import sys 
sys.path.append('/ Users / clanc / Documents / Development /')
导入测试


printline()



##(test.py文件中的函数printline
def printline():
## print 我正在工作是的,但是在什么文件中。我正在工作


解决方案

pyth_test 的定义声明在?它也位于被调用之前?



编辑:



要进行透视,请创建一个名为 test.py 的文件,其中包含以下内容:

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

pyth_test(1,2)

现在运行以下命令:

  python test.py 

您应该看到您输出的结果ire。现在如果你在一个交互式会话中,它应该像这样:

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

我希望这解释了声明的工作方式。



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


$ b myfunction.py

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

program.py

 #!/ usr / bin / python 

#我们的函数在这里被拉到
from myfunction import pyth_test

pyth_test(1,2)

现在如果您运行:

  python program.py 

它会打印出来3.现在解释出了什么问题,让我们用这种方式修改我们的程序:

 #Python:咦? pyth_test在哪里? 
#你说它在那里,但我还没有到那里呢!
pyth_test(1,2)

#我们的函数在这里被拉入
from myfunction import pyth_test

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

  $ python program.py 
Traceback(最近一次调用最后):
在< module>文件中的program.py,第3行。
pyth_test(1,2)
NameError:name'pyth_test'未定义



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



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

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

同样的过程适用。现在,导入包并不是那么简单,所以我建议您查看模块如何与Python一起工作。我希望这有助于你的学习并祝你好运!


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"

解决方案

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!

这篇关于函数在Python中没有定义错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆