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

chiu szu ting
chiu szu ting
2,318 Points

the loop making

just wonder is there a most proper way to write a loop? like in this case,i wrote another way of the while part:

    num=0
    while num<=3:            
        lists.append(string.format(**dicts[num]))
        num+=1
    print(lists)

this also worked,compare to the behind syntax,which is better?

strings.py
def string_factory(dicts,string):
    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}!"
           lists=[]
           num=(len(dicts))-1
    while num:
        lists.append(string.format(**dicts[num]))
        num-=1
        if num==0:
            lists.append(string.format(**dicts[0]))
    return(lists)


string_factory()          

1 Answer

William Li
PLUS
William Li
Courses Plus Student 26,868 Points

Hi there.

First thing first.

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}!"

This code template was provided by the code challenge as a sample test case, you should write your function under 'em in a new line, and not putting these block of code into the function definition.

Secondly,

    num=0
    while num<=3:            
        lists.append(string.format(**dicts[num]))
        num+=1
    print(lists)

This particular solution here, if it works (I haven't tested it yet), it will only work for the 1 sample test code written there, if the grader is trying to use another test case, it'll most definitely fail to pass. However, the idea here is absolutely correct, and the best, and most straight-forward way to go about writing the string_factory function is probably by using a for loop.

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(li, string):
    result = []                               # placeholder list that will become the return value of the function
    for item in li:                           # loop through each dictionary contained within the li list
        result.append(string.format(**item))  # unpack dictionary and pass 'em to string formatter then append to result list
    return result

Like such, hope it helps.

chiu szu ting
chiu szu ting
2,318 Points

thank you! it really helps!