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
Victoria Medley
17,085 PointsChallenge need help
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.
"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!"
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.
Here's my code..
def favorite_food(dict):
dict = {"name": "Victoria", "food": "tacos"}
return "Hi, I'm {name} and I love to eat {food}!".format(**dict)
And I keep getting this error.. Bummer! Expected favorite_food({'name': 'Michelangelo', 'food': 'PIZZA'}) to return "Hi, I'm Michelangelo and I love to eat PIZZA!", but got "Hi, I'm Victoria and I love to eat tacos!"
I tried switching the words to what they wanted and it didn't help, they just asked for a different sentence. I don't know what's going on here, it looks like my code is returning the line they're asking for?
2 Answers
Stuart Wright
41,120 PointsYour code is basically correct. You just need to remove this line:
dict = {"name": "Victoria", "food": "tacos"}
By setting the variable dict inside your function, you are overwriting whatever is passed in. You want your function to work for any appropriate dictionary which is passed in.
As an aside, using dict as a variable name isn't a good idea, since it is the name of a built-in type in Python. It does work for this challenge though.
Xayaseth Boudsady
21,951 PointsHere's a simpler way based on the video.
def favorite_food(dict):
return "Hi, I'm {name} and I love to eat {food}!".format(**dict)
Just use the ** (asterisk) to unpack your dictionary into those template.
J llama
12,631 Pointsnice one bruhhhh
Victoria Medley
17,085 PointsVictoria Medley
17,085 PointsThanks, that worked. Looks like I was overthinking it.