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 (2016, retired 2019) Dictionaries String Formatting with Dictionaries

Mauro Y.
Mauro Y.
6,520 Points

Didn't get all of the expected output from `string_factory()`.

Tested in my system and get the following list as requested: ["Hi, I'm Michelangelo and I love to eat PIZZA!", "Hi, I'm Garfield and I love to eat lasagna!"] So what is the rest of expected output? Something else?

string_factory.py
# Example:
# values = [{"name": "Michelangelo", "food": "PIZZA"}, {"name": "Garfield", "food": "lasagna"}]
# string_factory(values)
# ["Hi, I'm Michelangelo and I love to eat PIZZA!", "Hi, I'm Garfield and I love to eat lasagna!"]

#values = [{"name": "Michelangelo", "food": "PIZZA"}, {"name": "Garfield", "food": "lasagna"}]

def string_factory(values):
    dic = values
    i = 0 
    final = []
    while i < len(dic):  

        for value in dic[i].items():
            n = dic[i]['name']
            f = dic[i]['food']
            template = "Hi, I'm {} and I love to eat {}!".format(n,f )        

            if final == None or final == "": 
                final.append(template)
            else:
                final.append(template)

            i += 1

        return final

2 Answers

Christopher Shaw
seal-mask
PLUS
.a{fill-rule:evenodd;}techdegree seal-36
Christopher Shaw
Python Web Development Techdegree Graduate 58,248 Points

When I copied and pasted your code into a console, it gave lots of "IndentationError: unexpected indent". For example, the return statement at the end must line up with the while statement.

Also, there is no need for your 'if' statement, as both results are the same. So after a small tidy up, your code does pass:

def string_factory(values):
     dic = values
     i = 0 
     final = []
     while i < len(dic):  
         for value in dic[i].items():
             n = dic[i]['name']
             f = dic[i]['food']
             template = "Hi, I'm {} and I love to eat {}!".format(n,f )        
             final.append(template)
             i += 1
     return final
Mauro Y.
Mauro Y.
6,520 Points

Oh! Thank you very much for the correction. Besides the "IndentationError: unexpected indent", the bug was just in the 'if' statement?