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 trialJohn Schut
2,317 Pointsstrings.py solution
I made a solution which works in my workspace, but in the tool it's complaining about the update sequence being 1, while it should be 2. Beats me...
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(string, dicts):
new_list_of_strings = []
count = 0
for item in dicts:
dicts_sub = dict(dicts[count])
new_list_of_strings.append(string.format(**dicts_sub))
count += 1
return(new_list_of_strings)
2 Answers
Chris Freeman
Treehouse Moderator 68,441 PointsYour parameter order matters. The task says "Create a function named string_factory that accepts a list of dictionaries and a string
" in that order.
Your code for iterating the list of dicts is unusual.
for item in dicts:
dicts_sub = dict(dicts[count])
The for-loop assigns to item
each dict listed in dicts
and it can be used directly in place of dicts_sub
for item in dicts:
new_list_of_strings.append(string.format(**item))
John Schut
2,317 PointsHi Chris, thanks! I introduced the dicts_sub, because item directly seemed not to work eighter (but probably I did somwthing else wrong... :-) ).