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 trialChristian Glaß
5,650 PointsNo specific question, I just don't get this code challenge...
I tried to solve it in workspaces but no success, hope you can help me. :)
# 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(something):
2 Answers
Ryan Cross
5,742 PointsI think you should see these two parts of the language so you have them as tools in your toolbox. When you go on to do your own stuff you never know when iy will come in handy.
one interesting item is the **....for unpacking dictionaries in a function calls argument or packing kw pairs into a dictionary in function def
A second one is the specified placeholder format...like "This could help{name}later".format()
Viraj Deshaval
4,874 PointsTo understand this code challenge in a precise way, first just loop through each value in your values list.
values = [{"name": "Michelangelo", "food": "PIZZA"}, {"name": "Garfield", "food": "lasagna"}]
dict_values = []
for value in values:
dict_values.append(value)
#Output will be as below:
[{'name': 'Michelangelo', 'food': 'PIZZA'}, {'name': 'Garfield', 'food': 'lasagna'}]
Why we have used list instead of dictionary is because we are passing values to function in list form.
This gives you a hint that now by looping through each value and appending to your new list gives us a list with dictionaries. Now, only part left is formatting our template and for this we can use unpacking.
values_list = []
for value in values:
values_list.append(template.format(**value))
return values_list
Here we are using unpacking which means for each dictionary take its key and value and send it to the template as keyword arguments.
Hope this helps.