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 trialRoyce Sithole
2,625 Pointsstring_factory.py please help!!
hy guys its me again. ive been stuck on this problem for a few days now. heres what ive tried and ive commented out the logic that ive used. can someone tell me what im doing wrong.
heres is their question... Let's test unpacking dictionaries in keyword arguments. You've used the string .format() method before to fill in blank placeholders. If you give the placeholder a name, though, like in template below, you fill it in through keyword arguments to .format(), like this: template.format(name="Kenneth", food="tacos") Write a function named string_factory that accepts a list of dictionaries as an argument. Return a new list of strings made by using ** for each dictionary in the list and the template string provided.
# 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(my_list_of_dicts):
newlist = [] #this will create my new list
for dicts in my_list_of_dicts: #this will take each dict in the list of dicts
names = dicts[name] #this will set the variable name from a name in a dict
foods = dicts[food] #this will set the variable food from a food in a dict
newlist.append(template.format(name = names, food = foods)) #this will append the new string into newlist
return newlistlast #this will return the new list i have made
2 Answers
Steven Parker
231,269 PointsYou have a couple of errors in the return statement:
- it is indented too far — as it is, it returns during the first pass through the loop
- you return an undefined variable named newlistlast, but the list you create is named newlist
And the names of the keys should be in quotes, for example: dicts["name"]
. But while that will fix it, I believe the challenge is really intending for you to make use of the **
unpacking operator. In that case, you would not need the individual variables and could just do this:
newlist.append(template.format(**dicts))
Chris Freeman
Treehouse Moderator 68,441 PointsI see three errors:
the dict keys "name" and "food" should be in quotes
typo in return variable name. Should be
newlist
return is indebted to far. It is currently inside the
for
and will return at the end of the first iteration