Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Python Collections (2016, retired 2019) Dictionaries String Formatting with Dictionaries

what am i doing wrong in this dictionary unpacking... TypeError

need some help

string_factory.py
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
Oskar Lundberg
9,510 Points

All 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

thank you very much Oskar.. i didn't know it was that simple... i had to do it in another way...

Kent Åsvang
Kent Åsvang
18,823 Points

Remember 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.