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

Zachary Ruffner
Zachary Ruffner
16,451 Points

I am getting this error "Bummer! I couldn't import `string_factory`." String_factory challenge.

Hey guys,

I am working the string_factory challenge and it works fine in workspaces but I keep getting the "I couldn't import 'string_factory'" error.

Here is my code:

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

value = [ {"name" : "James", "food" : "peaches"}, {"name" : "Worf", "food" : "gaw"}, {"name" : "the gods", "food" : "ambrosia"},

]

def string_factroy(lst, string): new = [] for dict in lst: strint = string new.append(string.format(**dict)) return new

print(string_factroy(value, template))

Thanks for the help ahead of time,

Zachary

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}! "

value = [
    {"name" : "James", "food" : "peaches"},
    {"name" : "Worf", "food" : "gaw"},
    {"name" : "the gods", "food" : "ambrosia"},

]



def string_factroy(lst, string):
    new = []
    for di in lst:
        strint = string
        new.append(string.format(**di))
    return new

print(string_factroy(value, template))

1 Answer

Ronit Mankad
Ronit Mankad
12,166 Points

Hi! This is easy to implement:

def string_factory(args):
    args_as_list = list(args) #Since args will be of type tuple, we convert to list
    ls = []
    for dic in args_as_list:
        st = template.format(name = dic['name'], food = dic['food'])
        ls.append(st)
    return ls