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 Python Collections (Retired) Dictionaries String Formatting with Dictionaries

Sean Flanagan
Sean Flanagan
33,235 Points

String Formatting with Dictionaries

Hi.

I'm having no luck with this.

Clicking "Check work" returns the message "Bummer! What happened to string_factory?"

I'd be grateful for any help.

Sean

strings.py
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):
    for each_dict in dicts:
        string.format(**each_dict)
        new_list.append(string)

    return string

3 Answers

Julien riera
Julien riera
14,665 Points

Hello,

You can try this, it worked on my computer :

string = "Hi, I'm {name} and I love to eat {food}!"

def string_factory(dicts, string):
    new_list = []
    for each_dict in dicts:
        new_list.append(string.format(**each_dict))

    return new_list

Anyway, I assume that you need to use a variable to store the formatted string in order to, then, append it. I didn't try but I have something like this in mind, which should also work just fine:

string = "Hi, I'm {name} and I love to eat {food}!"

def string_factory(dicts, string):
    new_list = []
    for each_dict in dicts:
        formatted_string = string.format(**each_dict))
        new_list.append(formatted_string)

    return new_list

Let us know !

Cheers,

Julien

Sean Flanagan
Sean Flanagan
33,235 Points

Hi Julien.

Your first solution worked a treat. Thanks very much for your help.

Sean :-)

You're on the right track.

First off, new_list doesn't exist and you're adding something to something that doesn't exist??

To fix that, add new_list = [] at the beginning of the string_factory function.

Second of all, you returned the string, and not the list that the challenge expected.

Try returning new_list instead of the string.

I hope this helps :)

Ask me if you need more hints.

Good luck! ~alex

Sean Flanagan
Sean Flanagan
33,235 Points

Hi Alex.

I've altered my syntax and came up with this:

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(new_list, string):
    new_list = []
    for word in dicts:
        string.format(**word)
        dicts.append(string)

    return new_list

Error message: Bummer! format() argument after ** must be a mapping, not str

I think this is one of these things that hide in plain sight. If you have any more hints, I'd be very grateful.

Thanks for your help so far.

Sean

You were closer before.

My solution:

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 each_dict in dicts:
        string.format(**each_dict)
        new_list.append(string)
    return new_list

I hope this code makes sense :)

Good luck! ~alex

Sean Flanagan
Sean Flanagan
33,235 Points

Hi Alex. I put in the code:

def string_factory(dicts, string):
    new_list = []
    for each_dict in dicts:
        string.format(**each_dict)
        new_list.append(string)

    return new_list

and I got this message:

Bummer! Didn't get all of the expected output from `string_factory()`.