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

Evandro Luis Lima Pastor
Evandro Luis Lima Pastor
1,061 Points

What happening with string_factory?

Hello,

I'm so frustrated! I spend 2 hours to try to figure out what is wrong with my code.

The problem is with the sintax, logic or both? Any direction will be most appreciated. :)

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(string, dicts):
  for key, elem in disct.items():
    lista = {name:food}
    result = string.format(**lista)
    return result

I'm having trouble with the same exercise too. I saw that you spelled dicts wrong in you for loop. It also looks like you've indented your return too far in and made it part of your for loop.

2 Answers

Boris Ivan Barreto
Boris Ivan Barreto
6,838 Points

On this exercise, you should return a list

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):
  new_list = []    # define an empty list
  for items in dicts:
    new_string = string.format(**items)
    new_list.append(new_string)
  return new_list
Evandro Luis Lima Pastor
Evandro Luis Lima Pastor
1,061 Points

Hey @Anthony! Thanks for the tips... My code not work still. @Boris! The solution was so simple that I have to bang my head into the wall! Thanks for the tip. :D