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

Y. Kravets
Y. Kravets
10,350 Points

And one more about dictionaries

Well, clearly I do not understand dictionaries all that well, otherwise I bet the code below would be working. Apart from explaining what is it I am doing wrong could I please ask for a simple put explanation on using lists of dictionaries? I think I understand the concept, but the syntax and boundaries are unclear to me.

Thanks in advance!

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):
  counter = 0
  while counter < len(dicts):
    new_list.append(string.format(**dicts[counter]))
    counter += 1
  return new_list

2 Answers

Andre' Jones
Andre' Jones
26,671 Points

I think you understand Dictionaries well. The reason why this code is not working is because your using a variable inside your function that you haven't initialized.

As always I will let you figure out which variable that is

Y. Kravets
Y. Kravets
10,350 Points

I guess I should probably make a break for today because I seem to be missing very simple things such as declaring a new list. Yet again, thank you Andre for the proofread and your comment!