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 trialbehar
10,799 PointsI dont understand this at all
Im totally lost on this challenge. I dont know how to do it, an im not even sure what he wants me to do. I dont understand what the ** does aside from build the **kwargs argument for the function.
Can someone give me the solution and a take at an explanation?
# Example:
# values = [{"name": "Michelangelo", "food": "PIZZA"}, {"name": "Garfield", "food": "lasagna"}]
# string_factory(values)
# ["Hi, I'm Michelangelo and I love to eat PIZZA!", "Hi, I'm Garfield and I love to eat lasagna!"]
template = "Hi, I'm {name} and I love to eat {food}!"
def string_factory(**kwargs):
return ?????
2 Answers
Sneha Nagpaul
10,124 PointsYou want to look at all ways you can unpack (consider elements separately) dictionaries. A starting point would be to consider the input to string_factory(). It is not a dictionary. It's a list. So, that is not where the unpacking is required.
The unpacking would come later when you pass the dictionary to format().
Hope that helps!
David Dzsotjan
6,049 PointsHi! You have the template, containing the keywords in the curly braces (name and food). You want to construct a function that takes keyword arguments, and returns a string which is like the template, only the kwargs are substituted. So, a solution could be
def string_factory(**kwargs):
return template.format(**kwargs )
Try this, and check what it gives if you call
print(string_factory(name='Treetrunks', food='apple pie')