我试图用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"发布于 2011-05-13 11:09:58
在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发布于 2011-05-13 11:09:38
如果您展示了用于简单测试程序的代码,将会有所帮助。直接放入解释器中,这似乎是有效的。
>>> def pyth_test (x1, x2):
... print x1 + x2
...
>>> pyth_test(1, 2)
3
>>> 发布于 2014-10-19 01:04:39
如果使用已安装空闲版本的Python
>>>def any(a,b):
... print(a+b)
...
>>>any(1,2)
3https://stackoverflow.com/questions/5986860
复制相似问题