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

Greg Maddox
Greg Maddox
1,471 Points

I literally have no idea what to do here. There really isn't enough information in the video to help me finish this.

He sends a dictionary, in a literal way, as an argument in the function parameter. What do i do if i have no idea whats in the dictionary?

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

2 Answers

Nicholas Ward
Nicholas Ward
5,797 Points

You can return the key-value pair from the dictionary that's passed in by unpacking it with **. Example:

def function(dict):
    return "{letter} is a letter, and {number} is a number.".format(**dict)

function({"letter" : "A", "number" : 1})

# returns: 
# "A is a letter, and 1 is a number."

For the challenge, you just have to assume that the test will pass in a dictionary that looks something like this:

{"name" : "Kenneth", "food" : "pie"}
Greg Maddox
Greg Maddox
1,471 Points

Wow, thank you. I still don't know how that works, but i'm sure i'll get another chance at it.

Christopher Cunnyngham
Christopher Cunnyngham
748 Points

Agreed that the video didn't prepare for this task at all. Very frustrating.