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 trialChris van Beem
Courses Plus Student 26,647 PointsString_factory, Why doesn't this work?
i dont know why this doesnt work, im kinda stuck
# 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!"]
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):
result= []
for i in dicts:
result.append(string.format(**i))
return(result)
1 Answer
Stuart Wright
41,120 PointsYou're very close. The challenge doesn't ask you to pass a string into the function. All I had to do to get your code to pass was cut/paste your 'string = ...' line from outside the function to inside:
def string_factory(dicts):
string = "Hi, I'm {name} and I love to eat {food}!"
result= []
for i in dicts:
result.append(string.format(**i))
return(result)
Your function is actually more useful than the 'correct' solution since it allows different template strings to be passed in - it just wasn't quite what was asked.