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 (2016, retired 2019) Dictionaries String Formatting with Dictionaries

Im dumb

I fell so dumb, I really don't know how to solve this problem other way, i fell like I'm missing on something, but I have repeated previous video many times and still don't know how to solve it the way you accept it...

string_factory.py
values = [{"name": "Michelangelo", "food": "PIZZA"},
          {"name": "Garfield", "food": "lasagna"},
          {"name":"Daniil", "food":"when Pycharm is recognizing my name"}]
template = "Hi, I'm {name_in_temp} and I love {food_in_temp}!"

def string_factory(value):
    list_of_strings = []
    for dict in value:
        def appending(name=None, food=None):
            if name and food:
                list_of_strings.append(template.format(name_in_temp=name, food_in_temp=food))
        appending(**dict)
    return (list_of_strings)

string_factory(values)
Reyam Marcos
Reyam Marcos
Courses Plus Student 10,367 Points

this one works

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

def string_factory(list_of_dict):
    new_list = []
    for d in list_of_dict:
        new_list.append(template.format(**d))
    return new_list

wow man this is so much simpler, thanks ALOT, now i see how to use (**)

Chris Sederqvist
Chris Sederqvist
7,596 Points

I feel you. :) Some of these challenges makes me feel really stupid as well. You're not alone! My best advice is to keep going, never give up. When you hit a dead end, like this challenge, move on and get back to it later. Ask for advice, like you did, and when the solution arrives, don't move on until you truly understand how and why it works. I believe Reyam Marcos solved it below.

If you have trouble understanding the solution, try breaking it up into smaller pieces, like:

template = "Hi, I'm {name} and I love {food}"  

dic = {'name': 'Chris', 'food': 'Pizza'}       

def string_factory(d):                         
    return template.format(**d)                

print(string_factory(dic))

returns:

Hi, I'm Chris and I love Pizza

Good luck!

1 Answer

Steven Parker
Steven Parker
230,995 Points

You're solution is basically correct.

I think the issue is that the challenge was not expecting you to modify the template. Try changing your code slightly so that it uses the template as provided in the challenge.

Also, don't define your own values or call your function yourself. The challenge does these things internally (and most likely with values different from the example in the comments).