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

string_factory.py

help needed

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(val):
    template=[]
    for i in val:
        templatelist.append(template.format(**i))
    return templatelist

2 Answers

template = "Hi, I'm {name} and I love to eat {food}!"
def string_factory(val):
    templatelist = []
    for i in val:
        templatelist.append(template.format(**i))
    return templatelist
James Balliet
James Balliet
Courses Plus Student 1,273 Points

It would help if the video presentation would use code similar to this exercise in solving the problem. The presentation used examples that led me to believe this code would work... (This is pretty much the same as your example)

def favorite_food(name=None, food=None): return "Hi, I'm {} and I love to eat {}!".format(name, food)

favorite_food(**{"name": "James", "food": "Tacos"})

Jeff Muday
MOD
Jeff Muday
Treehouse Moderator 28,720 Points

Your code has a simple instantiation error, otherwise it's correct.

Note that you overwrote "template" with an empty list within the scope of your function. Change it to "templatelist" and your code should pass.

Best of luck with Python!