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

Gilang Ilhami
Gilang Ilhami
12,045 Points

String formatting with dictionaries

I tried doing the loop, but i just can't get it, am i missing something?

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):
    new_list = []
    for words in dicts:
        new_list = string.append(sting.format(**dicts))

    return new_list

print (string_factory(dicts, string))

2 Answers

Steven Parker
Steven Parker
217,443 Points

:point_right: You're closer, but you have a few syntax issues yet.

Ignoring the print statement at the end, which is not part of the challenge but doesn't hurt it, your remaining issues are all in the statement inside the loop:

        new_list = string.append(sting.format(**dicts))
  • you can't "append" a string, you probably want to append the new_list (instead of assigning to it)
  • you spelled "sting" (without the "r") where you apply format to it.
  • your loop extracts the individual dictionaries as "words", but the format still attempts to use dicts.
Gilang Ilhami
Gilang Ilhami
12,045 Points

Thank you steven, i was wondering if you can help me witht he 4th callenge?

def courses(my_dict):
    single = []
    for value in my_dict.values():
        single.append(value)
    return value