Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Python Collections (2016, retired 2019) Dictionaries String Formatting with Dictionaries

Joel Price
Joel Price
13,711 Points

string_factory() missing 1 required positional argument: 'dicts' error. I think I have the required argument, however.

This is what I've got. Can anyone see why I'm getting this?

Thank you.

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(string, dicts):
    holding = []
    for dict in dicts:
        holding.append(string.format(name=dict["name"], food = string["food"]))
    return holding

3 Answers

james south
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
james south
Front End Web Development Techdegree Graduate 33,258 Points

Joel Price almost there....you're calling the format method correctly, except you are calling it on string. what is string? you want to format the template and append that.

use holding.append(template.format(**dict))

james south
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
james south
Front End Web Development Techdegree Graduate 33,258 Points

you are close. your function should only take one argument, the list of dicts. then in your loop, the template formatted with the two asterisks and the loop variable is what you append to your new list.

Joel Price
Joel Price
13,711 Points

So I think I understand what you're saying and I've made a few changes.

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

def string_factory(dicts):
    holding = []
    for dict in dicts:
        holding.append(string.format(**dict))
    return holding

Only now I get a: I couldn't import string_factory. error. Is the problem still with my .append()?