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

string_factory() code seems correct, yet not passed test

On the test for string_factory() I added below code

def string_factory(in_dict, in_str): new_dict = list() for dict_item in in_dict: dict_item.update({'string': in_str.format(**dict_item)}) new_dict.append(dict_item) return new_dict

Which gives me below output on my console: [{'name': 'Michelangelo', 'food': 'PIZZA', 'string': "Hi, I'm Michelangelo and I love to eat PIZZA!"}, {'name': 'Garfield', 'food': 'lasanga', 'string': "Hi, I'm Garfield and I love to eat lasanga!"}, {'name': 'Walter', 'food': 'pancakes', 'string': "Hi, I'm Walter and I love to eat pancakes!"}, {'name': 'Galactus', 'food': 'worlds', 'string': "Hi, I'm Galactus and I love to eat worlds!"}]

Process finished with exit code 0

However, Treehouse console does not pass the code and gives below error: Didn't get all of the expected output from string_factory().

Maybe I did not get the question. What is expected here? If my understanding is correct, why does the code not work?

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(in_dict, in_str):
    new_dict = list()
    for dict_item in in_dict:
        dict_item.update({'string': in_str.format(**dict_item)})
        new_dict.append(dict_item)
    return new_dict

1 Answer

This issue is resolved now. I changed my code as below and that worked :)

def string_factory(in_list_of_dict, in_str): new_list = list() for dict_item in in_list_of_dict: new_list.append(in_str.format(**dict_item)) return new_list