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

Holden Glass
Holden Glass
6,077 Points

code challenge being stubborn, possibly.

I wrote this code according to the challenge and It checked out in workspaces(it printed out and didn't throw and exception) but it won't work in the challenge. It says the output of string_factory() should be a string. I don't know what else to do.

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(ld, s):
    for dit in ld:
        return str(s.format(**dit))

1 Answer

Steven Parker
Steven Parker
217,443 Points

:point_right: The challenge says "The output from string_factory() should be a list" (not string).

So you'll need a list in which you can collect the strings produced inside the loop, and then you will return only after the loop has completed. Also, formatting a string returns a string, so you won't need to convert it using str().

Also, are you aware that there is a newer version of this course?

Holden Glass
Holden Glass
6,077 Points

1) I just figured that out. Thank you though.

2) I am aware but I am doing this one because it is here and I will have an easer time with the newer one.