Python 为什么这个简单的三行程序不起作用?

Python 为什么这个简单的三行程序不起作用?,python,python-3.x,Python,Python 3.x,我检查了这个Youtube,在视频中显示它正在工作,但我无法运行它 class Pract: p1 = Pract() #creating an instance of Pract p1.age=45 #creating a field-variable print(p1.age) 在完成类的声明之前,不能实例化它。您在类中放置的所有内容都是类定义的一部分。您必须缩进代码以标记类定义的结尾。(1)请格式化代码。参见(2)什么是“不起作用”?错误消息是什么?类不是这样工

我检查了这个Youtube,在视频中显示它正在工作,但我无法运行它

class Pract:
    p1 = Pract()  #creating an instance of Pract
    p1.age=45     #creating a field-variable
print(p1.age)

在完成类的声明之前,不能实例化它。您在
类中放置的所有内容都是类定义的一部分。您必须缩进代码以标记类定义的结尾。

(1)请格式化代码。参见(2)什么是“不起作用”?错误消息是什么?类不是这样工作的。甚至不接近。你到底想干什么?@Aran Fey:我收到了一条错误消息“Pract not defined”,但还是得到了答案。上面的代码做了一些修改,我错过了pass语句&缩进(正如我在Youtube上看到的)。初学者,抱歉。不过谢谢你,谢谢安德里亚,正如我说的,我是个初学者。谢谢你的帮助。视频中没有显示写“通行证”。所以我错过了。谢谢
# First declare a class (empty, in this case)
class Pract:
    pass

# Then instantiate it
p1 = Pract()
# Then set the attribute
p1.age = 45
# Then print the attribute
print(p1.age)