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 trialemil sanchez
3,089 Pointswhat am i doing wrong in this dictionary unpacking... TypeError
need some help
def favorite_food(dict):
def unpack(name1=None, food1=None):
return name1, food1
name, food = unpack(**dict)
return "Hi, I'm {name} and I love to eat {food}!".format()
print(name)
print(food)
statement = favorite_food({"name": "eboni", "food": "burgers"})
2 Answers
Oskar Lundberg
9,534 PointsAll you need to do, is to tell the format function where to get the value for the name and food. If you unpack dict (**dict), in the format function. then it will look for the value of 'name' and 'food' in the dict that was passed in.
def favorite_food(dict):
return "Hi, I'm {name} and I love to eat {food}!".format(**dict)
This code passed the challenge, I hope this will help you :D
Kent Åsvang
18,823 PointsRemember this: a dictionary is assigned to a variable name, and the dictionary itself is made with keyword/value-pairs. Like so:
my_dictionary = {'keyword': 'value'}
if you print my_dictionary you get the dictionary as a whole:
print(my_dictionary)
>>> {'keyword': 'value'}
If you wan't to access only the value you need to give the dictionary the keyword for the value you wan't:
print(my_dictionary['keyword'])
>>> value
Also, you have a function unpack that you haven't defines.
Hope that helps you to complete the challenge.
emil sanchez
3,089 Pointsemil sanchez
3,089 Pointsthank you very much Oskar.. i didn't know it was that simple... i had to do it in another way...