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

Python

String 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
MOD
Chris Freeman
Treehouse Moderator 68,468 Points

Your original code does not use the item loop variable. Replace **dicts with **item as the format() argument.

This 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) 

Try def string_factory(*dicts, string):

It doesn't work. Now it says: string_factory() missing 1 required keyword-only argument: 'string'

What should I do?

I have changed it but it doesn't work: See here: http://pastie.org/private/1ggbmohjlxpit1piymeig

Thanks. It helped a lot.

Thanks for your help.