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

malfred koryor
malfred koryor
2,093 Points

unpacking string_factory

can someone please help me, I've been stuck for a while now.

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(name = None, food = None):
    if name and food:
        return(template)


string_factory(template.format(name = "john", food = "pizza"))

1 Answer

Hi, there!

I spot a few issues, and I think this is mainly because you misunderstood what the task wants you to do.

You are not trying to assign name or food, but your function should accept a list of dictionaries.

Like whats written in comments, say there is a list variable called values that contain multiple lists. your string_factory should take that variable value and do what task wants.

To help you get started. The task wants you to put your results into a list, so I would first start making a variable of an empty list. After that, I would use a for loop that takes items from the argument and format them into the template. Next, I would append that into the empty list you created. Lastly, you would return the list you created outside of the for loop.

If you find this to be ambiguous leave a comment and I will try to explain better. Hope this was helpful.

Patrick Madden
Patrick Madden
1,632 Points

I find this answer ambiguous since we technically didn't go over this in the video. I just started TreeHouse on Sunday, and I am beginning to understand why I am only in as far as I am and yet have more Python points than 72+% of other students enrolled in Python. For me personally, the lessons are not as intuitive to Beginners. I literally have no other experience than what I am learning through this site and books I've picked up on Python. The lesson for unpacking dictionaries doesn't get granular enough to drive this concept home. So when someone like myself or malfred get to the "code challenge", there's just not enough material previous to this challenge to help us understand or better yet, complete the code challenge and move onto the next portion with any confidence in our ability.