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 trialLuke Maschoff
820 PointsWhere am I going wrong here?
I'm supposed to format it, I followed the instructions and did what the video showed me to do.
def favorite_food(dict):
return "Hi, I'm {name} and I love to eat {food}!".format(name, food)
favorite_food(**{"name": "Luke", "food": "Pasta"})
2 Answers
Eric M
11,546 PointsHi Luke,
You only need to edit the favourite_food function, not create the caller. In favourite food we have a single paramater dict
that we'll unpack directly into the format function. The string in the format function is using named parameters {name}
and {food}
, so it expects these parameters to be in the dictionary that is unpacked into the calling of format.
The course does not explain this use of the format function and does not demonstrate this type of unpacking (although it is in the teachers notes) so I'm not surprised there's confusion.
Here is what it is asking for:
def favorite_food(dict):
return "Hi, I'm {name} and I love to eat {food}!".format(**dict)
The **
unpacks the dictionary it preceeds. This is demonstrated as being done in function calls or function definitions that you're writing, but you can of course use it in calls to other functions. Here we use it calling the built in format function.
Cheers,
Eric
joelearner
54,915 PointsHi Luke,
This challenge got me too. I tried many combinations like you did and I could not get it to go through.
If you would like to push through the code with a similar setup to what you have already, you will just have to rename the call name to pass in the kwargs like this:
def favorite_food(dict):
return "Hi, I'm {name} and I love to eat {food}!".format(**dict)
dict(**{"name": "Luke", "food": "Pasta"})
This might violate the DRY (don't repeat yourself) principle though because there are simpler ways to write the code and pass the challenge. Happy coding!
Cheers!
Eric M
11,546 PointsHi Joe, you should know that your line dict(**{"name": "Luke", "food": "Pasta"})
is not doing anything here.
The function favorite_food
's function signature is:
def favorite_food(dict):
So it is called with favourite_food(your_argument_here)
.
Your code passes the challenge because you correctly unpack any dictionaries containing keys of name
and food
that are passed to the favorite_food function. You do this when calling format on the return string, passing the unpacked version of dict using the double splat (**
) prefix i.e. .format(**dict)
.
For clarity, here are some examples of calling favourite_food.
example_dictionary = {"name": "example_name", "food": "example_food"}
introduction = favorite_food(example_dictionary)
print(introduction)
introduction = favorite_food({"name": "example_name", "food": "example_food"})
print(introduction)
These two examples will produce the same result. i've included a print statement so that you can paste these, and the favorite_food function into your ide and run the Python script for some imediate feedback. I do this a lot when learning and when building, just executing small chunks of code to confirm the output before going back to the larger problem.
Cheers,
Eric