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 trialDominic Malloy
1,857 PointsMethod Challenge Task - Task 2
I'm not sure where I'm going wrong in this task. When I run this in VS Code it produces no errors. Thank you in advance.
class Panda:
species = 'Ailuropoda melanoleuca'
food = 'bamboo'
def __init__(self, name, age):
self.is_hungry = True
self.name = name
self.age = age
def eat (self):
self.is_hungry = False
self.name = 'Bao Bao'
r = (f'{self.name} eats {self.food}.')
return r
1 Answer
Asher Orr
Python Development Techdegree Graduate 9,409 PointsHi Dominic!
Check out this error message:
Traceback (most recent call last):
File "", line 67, in test_results
AssertionError: 'Bao Bao eats berries.' != 'Charles eats berries.'
- Bao Bao eats berries.
+ Charles eats berries.
: Make sure to use the attributes already in the class. When I create an instance with Charles as the panda's name and change the food attribute to berries, I don't get the right message.
Since the challenge itself will run your code with 'Bao Bao' as the name attribute, you should delete this line:
self.name = 'Bao Bao'
When I ran your code after deleting that line, I still got an AssertionError (Regex didn't match).
I think it's because the task asks to return string, and not a variable.
Try returning a formatted f-String instead of the variable "r".
Let me know if that works!
Dominic Malloy
1,857 PointsHi Asher, that worked!. Thank you for taking the time to help.
And thank you for your response Jeff, appreciate it.
Jeff Muday
Treehouse Moderator 28,720 PointsJeff Muday
Treehouse Moderator 28,720 PointsAsher Orr is correct, and you are correct too!
The Challenge automatic grader is using a unit test based on the output of a regular expression. The regular expression is expecting only a
return '${self.name} eats {self.food}.'
So it will mark as incorrect other valid ways of completing this exercise.
The moral of this story is to try a Challenge, if you can't complete it with a reasonable effort then ask the community and move ahead.