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

Shih Kai-Wen
Shih Kai-Wen
613 Points

Challenge question

I am stuck in this challenge, can someone help me??

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}!"
def string_factory(food=None,name=None):
    if name and food:       
        new=list(template.format(name,food))
        return new

values(**[{"name": "Michelangelo", "food": "PIZZA"}, {"name": "Garfield", "food": "lasagna"}])

1 Answer

Afloarei Andrei
Afloarei Andrei
5,163 Points
  1. You'r function have to accept a list with dictionaries in it. So create a list like "values=[{......}, {....}] (copy the one commented in example), and don't forget to create the 'new = []'.
  2. You have to acces each dictionary at a time. For that you can use 'for dict in values:".
  3. Now you have to append the template formated with each dictionary value. "new.append(template.format(name = dict['name'], food = dict['food'])) "
  4. Finally return the 'new' list.

I hope I was clear enough for you to understand. :)