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

Idan shami
Idan shami
13,251 Points

I got lost in this challenge(string formatting with dictionaries)

I have no idea what to do, please help

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="Kenneth", food="tacos"}]):
    template = "Hi, I'm {name} and I love to eat {food}!".format(name, food)
    return template

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,441 Points

You're not far off. The challenge asks the function to accept a list of dictionaries. add a loop to use each dictionary in the list to format a new string. Append this string to a new list that will be returned after the loop completes.

Post back if you need more help. Good luck!!

Idan shami
Idan shami
13,251 Points

I still don't get it... they said "accepts a list of dictionaries as an argument" you said i didn't accept a list of dictionaries but i did... in the argument

def string_factory([{name="Kenneth", food="tacos"}]):

and I don't understand where I need to put two stars..... thanks, I hope you will help me again cause I still don't know what to do...

Chris Freeman
Chris Freeman
Treehouse Moderator 68,441 Points

It is a syntax error to use a literal object, such as a list, as an parameter.

>>> def string_factory([{name="Kenneth", food="tacos"}]):
  File "<stdin>", line 1
    def string_factory([{name="Kenneth", food="tacos"}]):
                       ^
SyntaxError: invalid syntax

The proper syntax is to use a variable name. That variable name will get assigned a list of dicts by the calling function, which in this case would be the challenge checker.

The form you seek is:

def string_factory(dict_list):
    for dct in dict_list:
        # process each dict
    return list of processed dicts
Idan shami
Idan shami
13,251 Points

Am I getting closer?

template = "Hi, I'm {name} and I love to eat {food}!"
values = [{"name": "Michelangelo", "food": "PIZZA"}, {"name": "Garlield", "food": "Lasagna"}]
list1 = []
def string_factory(values):
    for v in values:
        template = "Hi, I'm {name} and I love to eat {food}!".format(**v)
        template.append(list1)
    return list1

I don't understand how to append to list1, but tell me if I am right until now... thank you !! i don't know what would I done without you... good day.

Chris Freeman
Chris Freeman
Treehouse Moderator 68,441 Points

Getting closer, but need to fix:

  • as you have defined, template is a string which has no append() method. Instead append the template to list1:
list1.append(template)

Other changes to improve the code

  • it's unnecessary to define values as the challenge checker will set its value during testing
  • redefining template misses the point of using the template. It would have been better to use something like
new_string = template.format(**v)

then append new_string to list1.

  • while it does cause a error in this case (a bug in the challenge checker), list1 should be initialized inside the function.

I think you get it on this pass! Good luck!!

Idan shami
Idan shami
13,251 Points

it worked !!! thank you so much, I understand it better now! thank you so much!