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

string_factory.py

what do i need to do here? unclear

string_factory.py
# Example:
# values = [{"name": "Michelangelo", "food": "PIZZA"}, {"name": "Garfield", "food": "lasagna"}]
# string_factory(values)
# ["Hi, I'm Michelangelo and I love to eat PIZZA!", "Hi, I'm Garfield and I love to eat lasagna!"]

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

1 Answer

Im not sure wether you actually want the solved code? However the challenge asks you to create a function called string_factory that will take a single variable. This variable will be a list of dictonaires that has names and foods in them. Now it simply wants you to append the dictoniary content into the template provided and append this to a list. Once your done doing this for every dictonaire that the list contains, return the new list to the function. Please comment if you need actual code for this!

Cheers Behar, Id like the code please if you have it because I still cant figure it out. Thank you

template = "Hi, I'm {name} and I love to eat {food}!" # This is the template that he wants you to format the name and foods in.
def string_factory(dicts): # Here were defining the function that he wants us to use.
    new_list = [] # Here we are creating a new list, that each of the new templates.
    for name_and_food in dicts: # Here we are making a for loop for every keyword in the dictonary "dicts".
        new_template = template.format(name=name_and_food["name"], food=name_and_food["food"]) # Here we are creating a variable "new_template" and formatting the old template with the food and name of whatever keyword where currently on.
        new_list.append(new_template) # Here we append that new template to the list we made earlier.
    return new_list # Finally we reutrn the list.

Hope this helps!