【发布时间】:2011-05-13 03:05:17
【问题描述】:
我正在尝试在 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"
【问题讨论】:
-
如果你对函数的调用在第 1 行,那么函数定义在哪里?
-
抛出错误的代码看起来像是在交互式会话中,但函数代码看起来像是文件的一部分。那正确吗?如果是这样,您是如何将文件导入交互式会话的?请编辑您的问题并添加更多代码以显示完整的交互式会话。 (编辑按钮位于问题底部的标签下方。)