treehouse challenge- String Formatting with Dictionaries

Solving Complex Problems Through Programming. This course is also called advanced programming.
Forum rules
Please be careful about sharing personal information. Never use any identifiable information.
Post Reply
User avatar
Posts: 5
Joined: 24 May 2018, 08:46

treehouse challenge- String Formatting with Dictionaries

Post by Jake » 12 Nov 2018, 18:59


In treehouse, I'm currently at the part where dictionaries are introduced. Completed the challenge, but I don't really understand the whole "unpacking and packing" dictionaries thing, so I don't know if the code I wrote does that, or something else. It works, but again, I'm confused.

Here's the task:
You've used the string .format() method before to fill in blank placeholders. If you use a placeholder of {food} in the string, then you pass a keyword argument of food to .format(). The {food} placeholder in the string will be replaced with the value of the food keyword argument.

Complete the favorite_food function below. It accepts a dictionary as an argument. Your function should unpack that dictionary and pass it to the format method as keywords, then return the resulting string.

Code: Select all

"Hi, I'm {name} and I love to eat {food}!".format(name="Kenneth", food="tacos")
# Returns "Hi, I'm Kenneth and I love to eat tacos!"
And here's my code:

Code: Select all

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

1 x


Post Reply