Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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 67,987 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.