Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Basic Object-Oriented Python Welcome to OOP Creating a Panda Class

Daniel Mula Taranc贸n
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Daniel Mula Taranc贸n
Full Stack JavaScript Techdegree Graduate 37,873 Points

Hi everyone! I would like some help with my code.

I believe that the question maybe is not well defined.

The part in which I am interested is the init part. I think this should be the right answer:

def init(self, is_hungry): self.is_hungry = True

However, according to the question I can only put "self" inside the brackets. Either it doesn't make sense or I am wrong and I don't see it.

Thanks for helping me.

panda.py
# insert your code here
class Panda():
   species = 'Ailuropoda melanoleuca'
   food = 'bamboo'

def __init__(self):
    self.is_hungry = True

1 Answer

Hi Daniel!

In this case, you don't need to pass in an is_hungry parameter/argument because they just want the value of self.is_hungry to be True by default for every instance of the Panda class.

Of course, if you were making your own Panda class (in the real world), you could pass in an is_hungry parameter/argument, but if you did, you'd use it like this:

# insert your code here
class Panda():
    species = 'Ailuropoda melanoleuca'
    food = 'bamboo'

    def __init__(self, is_hungry):
        self.is_hungry = is_hungry # The passed-in value, not a hard-coded True


my_panda = Panda(False)

print( my_panda.is_hungry )

Which will print:

False

Code which I tested (and you can test, too, if you want) here: https://www.katacoda.com/courses/python/playground

Of course, in the real world, it would also make sense to do some kind of validation on is_hungry, to make sure the passed-in value is a boolean, and not any other data type.

You'll likely understand it better when you do this upcoming challenge:

https://teamtreehouse.com/community/init-argument-question

And for best results, make sure all your indentation is in multiples of 4 spaces precisely (don't use tabs), such as 4, 8, 12, 16, 20, etc. (Python is strict about it since it uses indentation instead of curly braces to determine/define a logical code block).

I hope that helps.

Stay safe and happy coding!