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 Python Collections (2016, retired 2019) Dictionaries String Formatting with Dictionaries

Emil Hejlesen
Emil Hejlesen
3,014 Points

Why isn't this correct?

can anyone explain?

string_factory.py
def favorite_food(name = None, food = None,):
    if name and food:
        return "Hi, I'm {} and I love to eat {}!".format(name, food)
    else:
        pass

favorite_food(**{"name": "emil", "food": "pizza"})

1 Answer

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,860 Points

Hi Emil. I received your request for assistance. :thumbsup:

Most times, the video teaches the concepts, but the challenges are not related to the video in the sense that the exact code there is not what the challenge wants, and I feel you are really overthinking the instructions, so there are a few issues going on here.

First, the challenge says to "complete the function... not modify it, which you did when you changed the parameter from dict. The function accepts a dictionary are the argument, so this will be something passed in after.

Second, The instructions do not ask for an if/else statement to check anything, so that needs to be deleted.

Next, you also modified it when you deleted the names in the place holders that are in the returned string. These are very important and are needed, so they will need to be put back

Finally, the instruction do not ask you to call the function, so that line needs also to be deleted. (The function will be called behind the scenes by the code checker.

Overall, instruction will always need to be followed exactly and explicitly. Never add what wasn't asked for, never change something that wasn't instructed to, and never delete ANY pre-loaded code in the challenges.

Now, for the challenge. It's best for you to restart the challenge to it's original pre-loaded state. Once you go through the explanation of what the challenge is looking for... the very last sentence is the instruction for what you should do. All you need for this challenge is to "unpack that dictionary and pass it to the format method", so you need only add one thing.

The parameter is named "dict" and remember the ** unpacks, so **dict is all that you have to pass into the format() method. That's all.

Hope that clears it up for you.
Keep Coding! :) :dizzy:

**Note: I also deleted the duplicate question you posted regarding this question. Please don't post multiple threads for the same question in the Community. This clutters the Community and just makes it confusing to properly navigate. Thanks.