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

"On my console it works, on the site it says "Bummer! 'name'"

This works on my computer, but it doesn't work on the site.

def string_factory(list_of_dictionaries, sentence):
    new_list = []
    for dictionary in list_of_dictionaries:
        new_list.append(sentence.format(dictionary['name'], dictionary['food']))
    return new_list

1 Answer

Hi Enrique.

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

because the string has keyword arguments for each value ie name and food, it expects them to be passed to the format method.

2 ways to do this

#example 1
def string_factory(list_of_dictionaries, sentence):
    new_list = []
    for dictionary in list_of_dictionaries:
        new_list.append(sentence.format(name=dictionary['name'], food=dictionary['food']))
    return new_list


#example 2

def string_factory(list_of_dictionaries, sentence):
    new_list = []
    for dictionary in list_of_dictionaries:
        new_list.append(sentence.format(**dictionary))
    return new_list