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 Adding to our Panda

Need help on the practice challenge

This is the Python practice challenge that requests to return the string which says "Bao Bao east bamboo". "Bao Bao" is the name attribute, and "bamboo" is the food attribute. I have been trying to add "Bao Bao" to the __init__ instance attribute, it gives me an error. But when I ran it in the console, it works...

Kindly advise if you know anything goes wrong below:

class Panda:
    species = 'Ailuropoda melanoleuca'
    food = 'bamboo'

    def __init__(self, name, age):
        self.name = "Bao Bao"
        self.age = age
        self.is_hungry = True

    def eat(self):
        self.is_hungry = False
        return ("{} eats {}".format(self.name, Panda.food))

[MOD: added ```python formatting -cf]

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,426 Points

You are very close. Errors to fix:

  • self.name should be set to the parameter value name and not a fixed string
  • the value of the class attribute food can be referenced using self.food
  • The return string needs an ending period.

Post back if you need more help. Good luck!!

Really appreciate your help! I did it in this way this time. But I am still receiving the error message as below: Use string formatting and backets ({}) to add the name and food attributes to your string. Make sure the message has the correct spelling and a period(.)

Also, where should I give the name "Bao bao" to the instance attribute "name"? Have tried several ways, but they arent working.

class Panda:
    species = 'Ailuropoda melanoleuca'
    food = 'bamboo'

    def __init__(self, name, age):
        self.name = name
        self.age = age
        self.is_hungry = True

    def eat(self):
        self.is_hungry = False
        #name = "Bao Bao" #self.name = "Bao Bao" (these ways are not working)
        return ("{} eats {}.".format(self.name, self.food))

[MOD: added ```python formatting -cf]

Chris Freeman
Chris Freeman
Treehouse Moderator 68,426 Points

Zee Liu, your new code should pass Task 2! unfortunately, the challenge checker is overly specific and is thrown off due to the extra parens surrounding the returned string. Change to:

        return "{} eats {}.".format(self.name, self.food)

I have submitted a bug fix to allow parens in the answer. Great work!!

By the way, this should also pass, but does not:

        return ("{} eats {}."
                "".format(self.name, self.food)

That worked! Also, have completed the third part of this task now.

Many thanks, Chris!