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

Keep getting the same 'use string formatting' error despite using it correctly each time. Any ideas?

The 2nd part of the Panda task keeps telling me to use string formatting even though that's what I'm doing. I've tried running this in another window and there are no issues with it. This is my code:

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
    return(f'{self.name} eats {self.food}.')

panda_one = Panda('Bao Bao', 3) panda_one.eat()

panda.py
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
        return(f'{self.name} eats {self.food}.')

panda_one = Panda('Bao Bao', 3)
panda_one.eat()

1 Answer

Cameron Childres
Cameron Childres
11,820 Points

Hi Sam,

Try putting a space between return and the rest of the statement. You can also drop the parenthesis if you like:

return (f'{self.name} eats {self.food}.')
# or 
return f'{self.name} eats {self.food}.'

I realize it works in workspaces without the space -- sadly I don't know enough Python to give a good explanation for the difference here.

That fixed it thanks!

Chris Freeman
Chris Freeman
Treehouse Moderator 68,454 Points

Python can be very forgiving when it comes to some built-in keywords. Since return isn't a function, it isn't expecting parens. Also, since the parens mark a harmless syntax grouping (and not a tuple), they are ignored. So space/no space or parens/no parens, python evaluates the statement the same. Another example of this syntax forgiveness is a space is accepted between print and the parens containing the arguments event though print is a function requiring parens. A space before a function parameter list within parens is tolerated for function calls in general, but is frowned upon as bad style.

The same is not true for the challenge checker which uses a regex to determine the "valid" code instead of an actual interpreter. Looking at a partial output of the original error. The checker outputs: Regex didn't match: 'return\\s\\(?f?.... Notice, the paren is optional \\(? and the formatted string is optional f?, but the space after the return is not.\\s. The space is suggested for style by PEP008 for readability, so I side with the space "requirement" on this one.

Cameron Childres
Cameron Childres
11,820 Points

Thank you for the explanation Chris!