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 trialAndriy Bobrovych
4,781 Pointswhat wrong?
what wrong in this code?
# 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(items):
result = []
for i in items:
result.append(template.format(**i))
print(result)
1 Answer
Krishna Pratap Chouhan
15,203 PointsIt's to be returned through the function and not printed my friend, you already solved it.
copy pasted the below content from my previous answer...
Yeah, its kinda tricky to understand but may be this would help you.
# 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(listt):
retString = [];
for key in listt:
retString.append( template.format(name=key["name"], food=key['food']));
return retString;
So get it done using '**' operator, we could use following code and then we don't have to specify what is "name" and "food" for the template.
def string_factory(listt):
ret_string = []
for key in listt:
ret_string.append(template.format(**key) );
return ret_string
string_factory(listt);
I dont know it would work, as i never tried before. But try searching the questions related to the topic, it would help you find your answers faster and you don't have to wait in case if someone has already asked it. I know this because i answered this few hours back. :). Have fun.
Hope this helps!
Andriy Bobrovych
4,781 PointsAndriy Bobrovych
4,781 Pointsthank you!! "return" helped =)