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

Natalia Henderson
Natalia Henderson
5,190 Points

I need help with this challenge

You've used the string .format() method before to fill in blank placeholders. If you use a placeholder of {food} in the string, then you pass a keyword argument of food to .format(). The {food} placeholder in the string will be replaced with the value of the food keyword argument.

"Hi, I'm {name} and I love to eat {food}!".format(name="Kenneth", food="tacos")

Returns "Hi, I'm Kenneth and I love to eat tacos!"

Complete the favorite_food function below. It accepts a dictionary as an argument. Your function should unpack that dictionary and pass it to the format method as keywords, then return the resulting string.

this is the code: def favorite_food(dict): return "Hi, I'm {name} and I love to eat {food}!".format() I don't know what to add or do, could someone help me?

string_factory.py
def favorite_food(**dict):
    return "Hi, I'm {name} and I love to eat {food}!".format(dict[name], dict[food])

2 Answers

Grigorij Schleifer
Grigorij Schleifer
10,365 Points

Hi Natalia, try to unpack the dict inside the format method using the **. You don't need to unpack dict in the method declaration.

Or you can modify your code and assign key and value to name and food "variables" like this:

def favorite_food(dict): 
    return "Hi, I'm {name} and I love to eat {food}!".format(name=dict['name'], food=dict['food'])

Does it make sense?

Grigorij Schleifer
Grigorij Schleifer
10,365 Points

Hey, Natalia, I am glad we could help! Ask again in the forum if you need assistance.

You have the right idea, just the **dict is in the wrong place (you don't need it in the function argument but somewhere else!)

Natalia Henderson
Natalia Henderson
5,190 Points

Thanks for helping me understand!