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
Nichole "Nikki" Carnesi
7,103 PointsString Factory Challenge
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
I'm stuck and have gone through the forum and still can't figure out what I'm doing wrong.
dicts = [
{'name': 'Michelangelo',
'food': 'PIZZA'},
{'name': 'Garfield',
'food': 'lasanga'},
{'name': 'Walter',
'food': 'pancakes'},
{'name': 'Galactus',
'food': 'worlds'}
]
string = "Hi, I'm {name} and I love to eat {food}!"
def string_factory(dicts, string):
string_list = []
for dict in dicts:
string_list.append(string.format(**dict))
return string_list
The error message I keep getting, regardless of how I change it (which has been far too many times now) is: Bummer! string_factory() missing 1 required positional argument: 'string'
2 Answers
james south
Front End Web Development Techdegree Graduate 33,271 Pointsif i remember correctly, the only argument to this function should be the list of dicts, not the template also. otherwise looks fine to me.
Thananjaya Chakravarthy
4,672 PointsGuys, Can anyone of you explain this code? Cause. I cannot understand that "why do we need to use ** for dict in append section
Chris Baldwin
Full Stack JavaScript Techdegree Graduate 43,358 PointsIt converts the dictionary ex: {"name": "Kenneth"} into key value pairs that can be used by the format ex: name = "Kenneth".
Nichole "Nikki" Carnesi
7,103 PointsNichole "Nikki" Carnesi
7,103 PointsTHANK YOU! You saved me from tears of frustration. Time for a break!