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 trialAndrew Wilcox
4,374 Pointsstring_factory.py
It just says Bummer! 'name' ????? I am not sure what I am doing wrong! I have try more than one approach and I can't unpack at the bottom, because it marks it incorrect too. I appreciate the help.
# 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}!"
values = [{"name": "Michelangelo", "food": "PIZZA"}, {"name": "Garfield", "food": "lasagna"}]
def string_factory(name=None, food=None):
while True:
name=name
food=food
if name and food:
return template.format(name, food)
string_factory({"name": "Michelangelo", "food": "PIZZA"}, {"name": "Garfield", "food": "lasagna"})
2 Answers
Mark Rinkel
13,501 PointsHey, there's a couple issues in your code.
Your function needs to take a list of dictionaries as it's input. Let's call those values.
def string_factory(values):
It asks us to return a new list of strings, so lets start by making an empty list
newStrings = []
Since we need to perform an action for each value passed to the function, lets use a loop. Then we can access the name and food properties of the dictionary individually.
for value in values:
name = value["name"]
food = value["food"]
And then we can use the format() function to create our string and append it to our newStrings list.
formattedString = template.format(name=name, food=food)
newStrings.append(formattedString)
Lastly we will return our newStrings list. Here's what my code looks like all together.
template = "Hi, I'm {name} and I love to eat {food}!"
def string_factory(values):
newStrings = []
for value in values:
name = value["name"]
food = value["food"]
formattedString = template.format(name=name, food=food)
newStrings.append(formattedString)
return newStrings;
Andrew Wilcox
4,374 PointsDear Mark,
Thanks for the feedback. I already solved the problem.
appreciate it!
Best, Andrew