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

PHILIP OBRIEN
PHILIP OBRIEN
1,165 Points

Why is my string_factory() function not passing?

I literally pasted this into workspaces and it worked. is there something I am not seeing?

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

def string_factory(values):
    stringa = values[0]
    stringb = values[1]
    return [template.format(**stringa), template.format(**stringb)]
template = "Hi, I'm {name} and I love to eat {food}!"

values =  [{"name": "Michelangelo", "food": "PIZZA"}, {"name": "Garfield", "food": "lasagna"}]

I think it might be wrong becouse if you add a third name and food in your "values" it does not work anymore

maybe try something like that:

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

1 Answer

PHILIP OBRIEN
PHILIP OBRIEN
1,165 Points

Thanks marta and AJ. Yeah I figured out that my code only worked for a list with two dictionaries. I needed the for loop. Also It wasn't for me to define the variable "values" I just needed to create the function and not pass anything into it. You guys are awesome for helping!

AJ Salmon
AJ Salmon
5,675 Points

Sounds like you grasped the concept pretty well, I'm glad you figured it out!!