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

Actually I am not able to understand this challenge, the concept is very new for me and I can get it well.

I would be very thankful if someone can explain this concept for me or at least provide some external resources. I tried to solve this challenge but I cant, I need to understand this topic first.

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 (name=None,food=None):
    if name and food:
        print (list ("Hi, I'm {} and I love to eat {}!".format(name,food)))
    else:
        print ("no action")


value=({"name": "Michelangelo", "food": "PIZZA"})
value2=({"name": "Garfield", "food": "lasagna"})

string_factory(**value)
string_factory(**value2)

1 Answer

Christopher Shaw
seal-mask
PLUS
.a{fill-rule:evenodd;}techdegree seal-36
Christopher Shaw
Python Web Development Techdegree Graduate 58,248 Points

Good day.

The values you are given is a dictionary, so the function takes one argument. We are going to return a dictionary, so we need to make an empty one to add to.

def string_factory (values_item):
    items_to_return = []

We then loop though the item supplied and unpack the values into the formatted sting, and append it to our new dictionary

    for item in values_item:
        items_to_return.append("Hi, I'm {} and I love to eat {}!".format(item['name'], item['food']))

finally we return (not print)

    return items_to_return

Thank you very much for your answer, I have tried it but I always get the message : string_factory() argument after ** must be a mapping, not list

by calling the function this way: values_item= [{"name": "Michelangelo", "food": "PIZZA"}, {"name": "Garfield", "food": "lasagna"}] string_factory(**values_item)

Please can you help me to find out why this is happening?

Regards,