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

Python Python Collections (2016, retired 2019) Dictionaries String Formatting with Dictionaries

Daniel Pye
Daniel Pye
2,725 Points

What's wrong with my string_factory code?

After reviewing the code step-by-step a few times, I still can't figure it out.

edit: Of course, the problem is with the for loop, but not sure what the correct syntax is for looping over the kwargs

string_factory.py
# 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(**strings):
    new_template = []
    for i in **strings:
        new_template.append(template.format(**i))
        return new_template

1 Answer

james south
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
james south
Front End Web Development Techdegree Graduate 33,271 Points

you only need the kwargs where you format the template and append, so you have that part fine, in the other two places you just need a regular parameter name. that holds the list of dicts so you can loop through them like you are. then your return statement is in the for loop so it needs to be moved over a tab to the left so that it only executes when the loop exits.

Daniel Pye
Daniel Pye
2,725 Points

Brilliant, thank you!