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 trialJothum Chitewe
7,833 Pointsstring_factory.py
help needed
# 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
Saulius Bartkus
89,219 Pointstemplate = "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
Jeff Muday
Treehouse Moderator 28,720 PointsYour 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!
Chris Freeman
Treehouse Moderator 68,441 Pointschanged comment to answer
James Balliet
Courses Plus Student 1,273 PointsJames Balliet
Courses Plus Student 1,273 PointsIt 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"})