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

Vedang Patel
Vedang Patel
7,114 Points

tried my best but it is getting very irritating

please try to solve it I've not yet tried it on workspaces but I'm sure its wrong because I didn't understand packing and unpacking

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(**dicts):
    result_list = []
    for item in dicts:
        result_list.append(string.format(dicts))
    return result_list

my_dict = {"name":"Kenneth", "food":"tacos"}
string_factory(my_dict)
template = "Hi, I'm {name} and I love to eat {food}!".format(**my_dict)

2 Answers

Vedang Patel
Vedang Patel
7,114 Points

I've go the answer thanks Mark but it wasn't what I needed but I understand you didn't do what the challenge asked me. You just read the comments which weren't what I needed.

template = "Hi, I'm {name} and I love to eat {food}!"
values = [{"name": "Michelangelo", "food": "PIZZA"}, {"name": "Garlield", "food": "Lasagna"}]

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

Hi

I am not up to speed on the specifics of the challenge ... but here is something that may point you to the right direction:

# 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(dicts):

    result_list = []

    for item in dicts:

        result_list.append(item)

    return result_list


my_dict = {"name":"Kenneth", "food":"tacos"}

string_factory(my_dict)

template = "Hi, I'm {name} and I love to eat {food}!".format(**my_dict)

print(template)

If his answers your question, please mark question as answered.

Thanks

Moderator edited: Markdown was added to properly render the code. Please see the Markdown Cheatsheet link at the bottom of the "Add an Answer" section for tips on how to post code to the Community.

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi Mark! Treehouse moderator here. I added some markdown to your code to make it a bit nicer to read. Thanks for your help in the Community! :sparkles:

Vedang Patel
Vedang Patel
7,114 Points

the function should return a new list of strings made by using ** for each dictionary in the list and the template string provided. so it didn't work and still don't have any idea Thanks!!!!!