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

Matte Matt
Matte Matt
2,354 Points

I dont know what to do

Help

string_factory.py
def favorite_food(*dict):
    return "Hi, I'm {name} and I love to eat {food}!".format(name, food)

2 Answers

Aaron Loften
Aaron Loften
12,864 Points

This took me FOREVER to figure out...

Okay, so....you unpack a dictionary with "**". A set of value key/pairs was passed to your function. Those values were grouped together by a value named "dict". To use them, you have to unpack it.

When you unpack it, it is an equivilant of physically typing out the values like "name=cat, food=pie" but saves you time because you just have to say "**stuff"

Does that make sense? I hope so. I feel like it could both make life simpler and extremely complicated....

The following code should pass. I didnt expect mine to pass so I just grabbed yours and modified it.

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

IF a moderator comes by, the video should probably mention unpacking a value inside of a function. It only mentions passing through an already unpacked value. That was very very confusing. I literally guessed to get it right after watching the video three times.

joshthorn
joshthorn
30,751 Points

The question asks to unpack a dictionary and pass it to .format.

def favorite_food(*dict):
    return "Hi, I'm {name} and I love to eat {food}!".format(**dict)
Matte Matt
Matte Matt
2,354 Points

That doesnt work.