简体   繁体   English

Python IDLE 回溯错误

[英]Python IDLE Traceback error

I am trying to learn Python on my own.我正在尝试自己学习 Python。 I created a Python file to to print Hello, world!我创建了一个 Python 文件来打印 Hello, world! My Python file (called test), which I did not save in the IDLE or Python library, is:我没有保存在 IDLE 或 Python 库中的 Python 文件(称为 test)是:

def hello():                                             
    print "Hello,world!"

When I ran this file in the Python shell as hello(),the error was:当我在 Python shell 中将这个文件作为 hello() 运行时,错误是:

Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
hello
NameError: name 'hello' is not defined

Can you help me troubleshoot the cause of this problem?你能帮我解决这个问题的原因吗?

I'm guessing your full code is something like this:我猜你的完整代码是这样的:

hello() # this hasn't been defined yet

def hello():
    println "Hello, world!"

When you call hello() on line 1, the function has not been defined yet because Python executes that script from the start to the end.当您在第 1 行调用hello()时,该函数尚未定义,因为 Python 从头到尾执行该脚本。 Move the function call below the function:将函数调用移到函数下方:

def hello():
    println "Hello, world!"

hello() # now Python knows what hello is

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM