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,878
  • 8
  • 61
  • 107

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
  • 398,270
  • 210
  • 566
  • 880
2

print is a function (see the docs):

You want:

...
def description(self):
    print(self.name)
    print(self.age)
...
Hamish
  • 22,860
  • 8
  • 53
  • 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,863
  • 7
  • 51
  • 80
2

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

It should be

print (self.name)
print (self.age)
karthikr
  • 97,368
  • 26
  • 197
  • 188