Problem in 6/18 - more on __init__() and self


#1
# 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

I have this as my code but it keeps on saying “Did you add the is_hungry attribute to init()?”, which I have. I am very confused and am starting to get a bit frustrated as I can’t continue even though I think I’m doing it correctly.

Could anyone help?

Thanks


#2

Hi @william0300,

The entire __init__ method needs to be indented, in order for it to be part of the Animal class.


#3

It still doesn’t work, I indented it like this
’’’

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
’’'
but it came up with this message
" File “python”, line 5
def init(self, name, age, is_hungry):
^
IndentationError: unexpected indent"

What shall i do now?


#4

To make it clear, I indented everything in the grey box once


#5

Your posted code is not completely formatted, so we cannot see some of its indentation. See How do I format code in my posts?.

Make sure that these two lines are indented by exactly the same number of spaces as the __init__ method header below them …

  """Makes cute animals."""
  # For initializing our instance objects

#6

Let’s try this then

# 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

#7

Codecademy currently uses a default of two spaces for each level of indentation for Python code.

It appears that in your code, the method header is indented by four spaces, while the two lines above it are each indented by the default of two spaces. The lines should match, therefore remove two spaces from the indentation of the method header, so that two spaces of indentation remain. Within the method, each line should have four spaces of indentation.


#8

For “def init(self, name, age, is_hungry):”, it is only indented once, so I don’t think I get what you mean by that, sorry. Could you please help edit my code if you can? Thanks

Btw, here’s the link if you need it:
https://www.codecademy.com/courses/learn-python/lessons/introduction-to-classes/exercises/more-on-init-and-self?action=lesson_resume&link_content_target=interstitial_lesson


#9

This is how the first several lines of your code should be formatted …

# 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

In the above code, the first two lines are not indented. The three lines following the class definition header are each indented by two spaces. That includes the __init__ method header. The three lines that follow the __init__ method header are each indented by four spaces.

Make sure that you use only spaces, and no tabs, for indentation for this exercise. Do not mix tabs and spaces for indentation in a Python program. For each program, choose one or the other, and be consistent regarding how many spaces or tabs to use for each level of indentation.

EDITED August 23, 2017 to include the first comment and the class definition header in the code above.


#10

The thing is, when I did

  """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

It came up with “File “python”, line 3
def init(self, name, age, is_hungry):
^
IndentationError: unexpected indent”

Is there something else I did wrong?


#11

Copy and paste the entirety of your code again. Make sure it is formatted. We need to check all of the indentation. This includes making sure that you used only spaces, and no tabs, for indentation.


#12
  """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

#13

Assuming that your code begins with these two lines, what you have posted above looks fine …

# Class definition
class Animal(object):

It appears that in your original post, tabs were used to indent these three lines …

	self.name = name
	self.age = age
	self.is_hungry = is_hungry

If you try to select the indentation that is displayed there, you may notice that you cannot select individual spaces. You can only select either all or none of the whitespace at the beginning of each of those three lines. That indicates that tabs are present there.

There was also the problem that this line was not indented …

def __init__(self, name, age, is_hungry):

If your code is still not working, it may be best to clean it up by first removing all of the indentation. Then carefully use spaces to insert the proper indentation again. Use only spaces to do so. Then you can be sure that there are no tabs.


#14

Thank you very much, it works now


#15

Excellent! :smiley:

Enjoy the rest of the course.


#16