我在 python shell 3.3.2 中运行该代码,但它给了我
SyntaxError: invalid syntax
。
class Animal(object):
"""Makes cute animals."""
is_alive = True
def __init__(self, name, age):
self.name = name
self.age = age
def description(self):
print self.name #error occurs in that line!
print self.age
hippo=Animal('2312','321312')
hippo.description()
我是Python新手,我不知道如何修复这些代码。谁能给我一些建议吗?预先感谢。
您正在使用
print
作为陈述。 Python 3 中不再是一条语句;现在它是一个函数。只需将其作为函数调用即可。
print(self.name)
print(self.age)
在 python 3 中,
print self.name
无效。
应该是
print (self.name)
print (self.age)