0

I run that code in python shell 3.3.2, but it gives me 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()

I'm a newbie in python and I don't know how fix that codes. Can anyone give me some advice? Thanks in advance.

Sayakiss
  • 6,668
  • 8
  • 53
  • 102

4 Answers4

3

print is a function in Python 3, not a keyword as it was in earlier versions. You have to enclose the arguments in parentheses.

def description(self):
    print(self.name)
    print(self.age)
Bill the Lizard
  • 386,424
  • 207
  • 554
  • 861
2

print is a function (see the docs):

You want:

...
def description(self):
    print(self.name)
    print(self.age)
...
Hamish
  • 22,072
  • 7
  • 50
  • 67
2

You're using print as a statement. It's no longer a statement in Python 3; it's a function now. Just call it as a function and you should be all set.

print(self.name)
print(self.age)
Henry Keiter
  • 16,324
  • 7
  • 47
  • 78
2

In python 3, print self.name is not valid.

It should be

print (self.name)
print (self.age)
karthikr
  • 92,866
  • 25
  • 190
  • 186