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 (Retired) Dictionaries String Formatting with Dictionaries

Code Challenge: String Formatting with Dictionaries

I think so far I'm not really sure I'm understanding coding in the first place unfortunately, but I'm stuck on this challenge. I'm not entirely sure what the question is asking I guess, but I'm lost. It says I'm not getting expected output from string_factory. Can someone hint me at what to do next?

strings.py
dicts = [
    {'name': 'Michelangelo',
     'food': 'PIZZA'},
    {'name': 'Garfield',
     'food': 'lasanga'},
    {'name': 'Walter',
     'food': 'pancakes'},
    {'name': 'Galactus',
     'food': 'worlds'}
]

string = "Hi, I'm {name} and I love to eat {food}!"

def string_factory(dicts, string):
  strings = []
  for x in strings:
    string.format(**dict)
  return strings

print(string_factory(dicts, string))

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,441 Points

There are a few things to correct.

  • the parameter dicts is a list of dictionares. The for loop should iterate over the dict list.
  • The string created in the for loop isn't being appended to the strings list
  • print statement unccessary
def string_factory(dicts, string):
    strings = []
    for d in dicts: #<-- iterate over dict list
        strings.append(string.format(**d)) #<-- Append the new string to the strings list
      return strings

Thank you! Now I understand a bit more.