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 trial
alexandracoder0809
2,606 PointsString Formatting with Dictionaries Code Challenge
Hi,
Excuse me for asking again, but I want really to learn what I am doing right and wrong: Here is my code:http://pastie.org/private/dqrab8spjxeofjxz2a
And it gives me that error message:
new=string.format(**dicts)
TypeError: format() argument after ** must be a mapping, not list
What I am doing wrong here? Am I on the right way for the soloution?
P.S. Is it "normal" that I ask more question in these sections than in the Basics because I find myself struggling harder as further I get. I need some cheering up:)
Thanks in Advance, Alexandra
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):
liste1=[]
for item in dicts:
new=string.format(**dicts)
liste1.append(new)
return liste1
[MOD: copied code from pastie.org -cf]
7 Answers
Chris Freeman
Treehouse Moderator 68,468 PointsYour original code does not use the item loop variable. Replace **dicts with **item as the format() argument.
Avi Tsipshtein
7,461 PointsThis bit of code worked for 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(dicts, string):
new_list = []
for key in dicts:
new_item = string.format(**key)
print(new_item)
new_list.append(new_item)
return new_list
print(new_list)
string_factory(dicts, string)
Cindy Lea
Courses Plus Student 6,497 PointsTry def string_factory(*dicts, string):
alexandracoder0809
2,606 PointsIt doesn't work. Now it says: string_factory() missing 1 required keyword-only argument: 'string'
What should I do?
alexandracoder0809
2,606 PointsI have changed it but it doesn't work: See here: http://pastie.org/private/1ggbmohjlxpit1piymeig
alexandracoder0809
2,606 PointsThanks. It helped a lot.
alexandracoder0809
2,606 PointsThanks for your help.