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

Dwayne Thomas
Dwayne Thomas
720 Points

May I see the answer for the strings.py exercise?

Here's my code's result, which seems unexpected to the task checker. "Hi, I'm Michelangelo and I love to eat PIZZA! Hi, I'm Garfield and I love to eat lasanga! Hi, I'm Walter and I love to eat pancakes! Hi, I'm Galactus and I love to eat worlds!"

3 Answers

Mikael Enarsson
Mikael Enarsson
7,056 Points
def string_factory(some_dicts, a_string):
  r_list = []
  for item in some_dicts:
    r_list.append(a_string.format(**item))
  return r_list

Out of interest, could you post your code?

Dwayne Thomas
Dwayne Thomas
720 Points
def string_factory(dictionary, string):
    i = 0
    name_foods_formatted = ""
    while i < len(dictionary):
        name_foods_formatted += string.format(dictionary[i]['name'], dictionary[i]['food'])
        i += 1
    return name_foods_formatted

This code was graded as incorrect.

Dwayne Thomas
Dwayne Thomas
720 Points

Thanks Mikael:-), your response helped my spot my mistake. I was returning a string instead of a list.