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 trialTodd Bascombe
Python Web Development Techdegree Student 3,472 Pointsi tried everything , i give up how to solve this problem
help with unpacking the dictionary
def favorite_food(**info):
return "Hi, I'm {name} and I love to eat {food}!".format(**info)
2 Answers
Brandon Spangler
8,756 PointsThe 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.
Chris Freeman
Treehouse Moderator 68,441 PointsWhen 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])
Todd Bascombe
Python Web Development Techdegree Student 3,472 Pointsthank you that was very helpful
Chris Freeman
Treehouse Moderator 68,441 PointsChris Freeman
Treehouse Moderator 68,441 PointsMoved comment to answer.
Todd Bascombe
Python Web Development Techdegree Student 3,472 PointsTodd Bascombe
Python Web Development Techdegree Student 3,472 Pointsthank you., you are very helpful and supportive