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 trialdaniel refael
1,679 PointsHi, how can I return the string, and which way? did I just need to write the sentence?
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'
str = self.name + 'eats' + Panda.food
return str
3 Answers
Caleb Kemp
12,754 PointsSo, it is looking for the answer in a very particular format, something as small as an extra space, or no space where it was expecting one, will cause it to fail.
The def eat(self):
method should only have two lines of code.
- set value of is_hungry (which looks good on yours)
- return statement using formatted string.
Here is an example of what formatted string looks like
return (f'{self.var1} text {self.var2}')
At minute 2:00 here is where the teacher demonstrates printing a formatted string.
I know it's super annoying, but make sure you get it to print out the challenge text exactly. Since you can't see the output of your code in the challenge, and the hints aren't always super helpful, I find it sometimes helps to first complete the challenge in the workspace. That way you know it works, and you just need to play around with the formatting, versus wondering if there is actually something wrong or a typo that is causing it to fail. Hope that helps
P.S. One more thing, although in this case it will print the same result, use "self.food", NOT "Panda.food", since the challenge is expecting "self.food", using "Panda.food" will cause it to fail.
Steven Parker
231,172 PointsThere are several issues here:
- the method should not change the value of
self.name
- the string
eats
needs spaces to separate it from the name and food - a class should not refer to itself by name (like
Panda.food
), but by using the keywordself
- the output string should end with a period (
.
)
But according to the error message, even fixing these errors isn't enough. The challenge is looking for code that builds the output as a formatted string ("f"-string) and returns it without creating another variable. So apparently, this exact line must be used for building and returning the result:
return f"{self.name} eats {self.food}."
If you consider this a bug in the challenge (I would), you may want to report this to Support. If they agree, it will get you a "special Exterminator badge".
Caleb Kemp
12,754 PointsI remember when it seemed like most challenges were like that, where the code had to be in an exact format down to the spacing, for example
var x = 5;
would fail for
var x = 5;
I remember one particular JavaScript challenge which left me stumped for hours. I had built and confirmed that the code was functioning exactly as intended. The challenge wasn't giving me an "expecting grep" statement, and I was convinced mine was formatted identically like the example. What was the problem? I had used "tabs" to create the spacing, the challenge (while visually the same), was expecting exactly 3 "space" characters everywhere I had used the tab key. Doing the challenges I remember always thinking, I'm not sure if this challenge is super helpful in helping me learn language x, but it certainly is making me more proficient at reading grep statements lol I don't miss the old challenges.
p.s. I like the way you explained your solution. Also, is it just me, or did you also feel that those formatting difference rejections were frustrating at times? Probably just me
Steven Parker
231,172 PointsI don't recall challenges being this specific in the courses I've taken. I think they mostly accepted any method that achieved the required results, which seemed more appropriate.
Caleb Kemp
12,754 PointsThat's good to hear
daniel refael
1,679 PointsThank you all !!!