0

我在 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的新手,我不知道如何修复这些代码。谁能给我一些建议?提前致谢。

4

4 回答 4

3

print是 Python 3 中的函数,而不是早期版本中的关键字。您必须将参数括在括号中。

def description(self):
    print(self.name)
    print(self.age)
于 2013-06-05T02:30:30.763 回答
2

print是一个函数(参见文档):

你要:

...
def description(self):
    print(self.name)
    print(self.age)
...
于 2013-06-05T02:30:28.530 回答
2

您正在使用print作为声明。它不再是 Python 3 中的语句;它现在是一个功能。只需将其作为函数调用,您就应该准备就绪。

print(self.name)
print(self.age)
于 2013-06-05T02:30:29.827 回答
2

在 python 3 中,print self.name无效。

它应该是

print (self.name)
print (self.age)
于 2013-06-05T02:30:40.333 回答