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

i tried everything , i give up how to solve this problem

help with unpacking the dictionary

string_factory.py
def favorite_food(**info):

    return "Hi, I'm {name} and I love to eat {food}!".format(**info)

2 Answers

Brandon Spangler
Brandon Spangler
8,756 Points

The function takes a dictionary. ** or * is meant to unpack a dictionary or a tuple. You want your function to be defined like this: def favorite_food(info): ... the rest is correct. Don't give up, if all else fails, look up what ** and * actually do.

thank you., you are very helpful and supportive

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,457 Points

When using **info as a function parameter it means the function accepts any number of keyword arguments and puts them into a dictionary named β€œinfo”. When using this Python idiom, it is customary to uses β€œkwargs” as the parameter name as a helpful hint to what is going on.

Since the challenge is passing in a single dictionary as a positional argument, using the kwargs-type placeholder fails because it is not expecting positional arguments.

If you were to use *info to accept any number of positional arguments then the dictionary passed in would be placed into a list named info as the first element. For comparison, the following code would pass:

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

thank you that was very helpful