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 trialLeen Leenaerts
Courses Plus Student 2,367 Pointswhat's wrong?
?
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):
l = []
for i in dicts:
string = string.format(**i)
l.append(string)
return l
2 Answers
lambda
12,556 PointsIf you open a workspace and add a print to the bottom of your code:
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):
l = []
for i in dicts:
string = string.format(**i) # line 1
l.append(string) # line 2
# try to combine line 1 and line 2 into 1 single line.
return l
# print output
print(string_factory(dicts, string))
Your output is repeating the same line over and over:
["Hi, I'm Michelangelo and I love to eat PIZZA!", "Hi, I'm Michelangelo and I love to eat PIZZA!", "Hi, I'm Michelangelo and I love to eat PIZZA!", "Hi, I'm Michelangelo and I love to eat PIZZA!"]
It should be:
["Hi, I'm Michelangelo and I love to eat PIZZA!", "Hi, I'm Garfield and I love to eat lasanga!", "Hi, I'm Walter and I love to eat pancakes!", "Hi, I'm Galactus and I love to eat worlds!"]
Leen Leenaerts
Courses Plus Student 2,367 PointsWhat is the reason for this? I really don't see why
Leen Leenaerts
Courses Plus Student 2,367 Pointsok I got why thanks