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

Pitrov Secondary
Pitrov Secondary
5,121 Points

I am confused, I do not clearly understand what I need to do here, and I do not know how I can get it right, I need help

I am doing this challenge and I am not sure what I have to do. Can you help me?

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

1 Answer

Bruce Röttgers
Bruce Röttgers
18,211 Points

Hey, you need to rewrite the function, so that it fills in name with the value of the key name (and food) of the dictionary.

You have rewritten the function declaration, which you aren't allowed to do in the challenge. The function should accept a dictionary and your job is to pull out the values and insert them in the print statement using the format() function.

Pitrov Secondary
Pitrov Secondary
5,121 Points

I do not understand... I can't get it to work. here's what I got

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

favorite_food(**{'name':'Rolph', 'food': 'Pizza'})
Bruce Röttgers
Bruce Röttgers
18,211 Points

You shouldn't call the function, that often breaks the challenge. Delete the function call and try again.

Pitrov Secondary
Pitrov Secondary
5,121 Points

So I need to do the challenge without calling the function? So I just needed to paste in what he already had written, I tried that, but that didn't work either. Sorry if the solution is obvious, cause I have no idea.

Bruce Röttgers
Bruce Röttgers
18,211 Points

No, my bad I messed up. You need to insert the appropriate values in the format function, (value for key in dictionary)

Pitrov Secondary
Pitrov Secondary
5,121 Points

I changed my code a little bit, and now I do not see what is wrong with it. Here's what I've got

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

favorite_food(**{'name':'Rolph', 'food': 'Pizza'})
Bruce Röttgers
Bruce Röttgers
18,211 Points

YOURE NOT ALLOWED TO CHANGE THE FUNCTION PARAMETERS

Pitrov Secondary
Pitrov Secondary
5,121 Points

here is what I've got now.

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

favorite_food(**{'name':'Rolph', 'food': 'Pizza'})

What is wrong with this code?