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

Todd Anderson
Todd Anderson
4,260 Points

I do not understand the question. Could it be worded in a different way?

I'm not exactly sure what the question is asking me. I've tried a bunch of different solutions and re-watched the videos prior to the challenge, but that did not help me get through this.

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(*values):
    new_list = []
    if name and food:
        new_list.append(template.format(**args))
        return new_list

Hi Todd,

You don't want an IF statement there, you want a for loop to cycle through each dict in the list. I try and comments at each step so I know what I've got so far. e.g.

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

def string_factory(values):
#I've just passed through the list of dicts - values (no need for the asterix *)
    new_list = []
    for dicts in vlaues:
    #for each item in the vlaues list do the following
        #append that new list with **dicts
    #return that new list (check indentation)

Good luck mate!

2 Answers

Steven Parker
Steven Parker
231,275 Points

The instructions are very specific, and your issues seem to be more about basic structure. Perhaps a few hints will help:

  • you won't need a packing operator ("*") with the parameter
  • you don't need to change (un-comment) any of the provided code and comments
  • you don't need a conditional ("if"), but you will need a loop
  • the append/template line looks good but you didn't define "args" yet
  • the return should always be done last (it is indented too far)
Todd Anderson
Todd Anderson
4,260 Points

Thank you.

I originally tried a loop as well and got an error that not all of the items were found. This example is vague considering watching all of the videos prior did not help me solve the challenge. In the video right before this challenge he uses *kwargs in the parameter. So by doing that and then asking to separate items with * it is confusing as to what exactly needs to be done. So far an example has not left me feeling this lost.

I figured it out, I just think that the wording is confusing the way it is written. Also, I did not know you could use dicts as an iterator. Thanks James.

Steven Parker
Steven Parker
231,275 Points

Now that you've solved it, can you think of a better way to phrase the instructions?