Python Python Collections Dictionaries String Formatting with Dictionaries

emil sanchez
emil sanchez
2,964 Points

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
6,524 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

emil sanchez
emil sanchez
2,964 Points

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,810 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.