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

Bentley Thompson
Bentley Thompson
13,287 Points

Could you tell me what is wrong with this code? It runs in my Workspace but is giving me an error in the exercise window

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

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

def string_factory(thedicts, astring): alist = [] for eachdict in thedicts: alist.append(string.format(eachdict['name'], eachdict['food'])) return alist

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

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

def string_factory(thedicts, astring):
  alist = []
  for eachdict in thedicts:
    alist.append(string.format(eachdict['name'], eachdict['food']))
  return alist

print(string_factory(dicts, string))

2 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,454 Points

There are two errors to fix:

  • use parameter name astring instead of "string" in alist.append() argument
  • add keywords to format argument list
def string_factory(thedicts, astring):
  alist = []
  for eachdict in thedicts:
    alist.append(astring.format(name=eachdict['name'], food=eachdict['food']))
  return alist