Traceback (most recent call last): File "python", line 14, in <module> TypeError: __init__() takes exactly 3 arguments (4 given)


#1
# Class definition
class Animal(object):
  """Makes cute animals."""
  # For initializing our instance objects
  def __init__(self, name, age):
    self.name = name
    self.age = age

# Note that self is only used in the __init__()
# function definition; we don't need to pass it
# to our instance objects.

#zebra = Animal("Jeffery", 1, False)
giraffe = Animal("Bruce", 1, False)
panda = Animal("Chad", 7, True)

#print zebra.name, zebra.age, zebra.is_hungry
print giraffe.name, giraffe.age, giraffe.is_hungry
print panda.name, panda.age, panda.is_hungry


#2

giraffe.is_hungry

where does is_hungry come from?

read the comment in the code editor

# Note that self is only used in the __init__()
# function definition; we don't need to pass it

so right now your function has three parameter but you passed four arguments.

giraffe = Animal("Bruce", 1, False)

where does the last argument go?


#3
# Class definition
class Animal(object):
  """Makes cute animals."""
  # For initializing our instance objects
  def __init__(self, name, age,is_hungry):
    self.name = name
    self.age = age
    self.is_hungry = is_hungry
# Note that self is only used in the __init__()
# function definition; we don't need to pass it
# to our instance objects.

zebra = Animal("Jeffrey", 2, True)
giraffe = Animal("Bruce", 1, False)
panda = Animal("Chad", 7, True)

print zebra.name, zebra.age, zebra.is_hungry
print giraffe.name, giraffe.age, giraffe.is_hungry
print panda.name, panda.age, panda.is_hungry


#4

:joy::joy::joy: 哈哈哈哈哈哈哈哈


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.