python新手问题
简明PYTHON教程中有这样一个例子:
- Python code
class Person: def sayHi(self): print 'hello,how are you?' p=Person() p.sayHi()
在2。7的IDLE下输入上述后第一个回车是个空行,第二个回车后提示:
- Python code
Traceback (most recent call last): File "<pyshell#30>", line 1, in <module> class Person: File "<pyshell#30>", line 4, in Person p=Person()
NameError: name 'Person' is not defined
Person的类没有找到或没有定义??/
[解决办法]
别偷懒。如果你实际的代码,和发给别人看的代码不一致,就是在浪费大家的时间了。这是性质非常恶劣的一件事情。
你这个缩进问题就很大。修改如下:
- Python code
class Person: def sayHi(self): print 'hello,how are you?'#如果你是在python的交互解释器中黏贴代码,请务必在class和后继代码中留空行。p=Person()p.sayHi()
[解决办法]
你这样写是不对的
试试这么写:
- Python code
class Person: def sayHi(self): print 'hello,how are you?'#如果你是在python的交互解释器中黏贴代码,请务必在class和后继代码中留空行。if "__main__" == __name__: p=Person() p.sayHi()
[解决办法]
- Python code
class Person: def sayHi(self): print 'hello,how are you?'然后再回车,输入下边的内容p=Person()p.sayHi()
[解决办法]
每一级缩进代表一个作用域,你仔细对比上下两个缩进看是否配对