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 trialErik Nemcik
1,569 PointsString formatting with dictionaries
Could you please tell me what is wrong here?
# 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 = [{"name": "Michelangelo", "food": "PIZZA"}, {"name": "Garfield", "food": "lasagna"}]
def string_factory(dicts, strings):
newlist = []
for d in dicts:
newitem = strings.format(**d)
newlist.append(newitem)
return newlist
string_factory(template)
2 Answers
Christian Mangeng
15,970 PointsHi Erik,
1) The function only accepts one argument, which is a list of dictionaries ("dicts" in your example), so delete the "strings" parameter.
2) The string you want to format is the "template" string, so newitem needs to be defined as:
newitem = template.format(**d)
3) You do not need to call the function at the end.
Hope that helps!
Arturo Mascarinas
4,037 PointsHi Erik,
I don't know if you solved it or not, but I took a look at this, because I was having a hard time trying to figure out where to begin.
I actually worked on it in Workspaces because the challenges are difficult to debug.
Like Christian said earlier, this function only takes one argument. Also you need to keep the template which was stated in the challenge:
template = "Hi, I'm {name} and I love to eat {food}!"
You just about have it. Let's just rename your dictionary list to make it easier:
values = [{"name": "Michelangelo", "food": "PIZZA"}, {"name": "Garfield", "food": "lasagna"}]
template = "Hi, I'm {name} and I love to eat {food}!"
values = [{"name": "Michelangelo", "food": "PIZZA"}, {"name": "Garfield", "food": "lasagna"}]
def string_factory(new_list):
newlist = []
for d in new_list:
newitem = template.format(**d)
newlist.append(newitem)
return newlist
string_factory(values)
The key line is newitem = template.format(**d). In this line you are unpacking the dictionary in the list and then taking the values name and food in the dictionary, and replacing them in the variable name holders {name} and {food} in the template.
I tested this in Workspaces and named it test.py. In order to see the output, I changed the line return newlist to print(newlist) so I could just see the output.
treehouse:~/workspace$ python test.py
["Hi, I'm Michelangelo and I love to eat PIZZA!", "Hi, I'm Garfield and I love to eat
lasagna!"]