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 trialZee Liu
2,027 PointsNeed 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
Treehouse Moderator 68,441 PointsYou are very close. Errors to fix:
-
self.name
should be set to the parameter valuename
and not a fixed string - the value of the class attribute
food
can be referenced usingself.food
- The return string needs an ending period.
Post back if you need more help. Good luck!!
Zee Liu
2,027 PointsZee Liu
2,027 PointsReally 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.
[MOD: added ```python formatting -cf]
Chris Freeman
Treehouse Moderator 68,441 PointsChris Freeman
Treehouse Moderator 68,441 PointsZee 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:
Zee Liu
2,027 PointsZee Liu
2,027 PointsThat worked! Also, have completed the third part of this task now.
Many thanks, Chris!