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

Dalton Dunn
Dalton Dunn
6,709 Points

Tuple index out of range?

I've been racking my brain on this for a day now. Usually can google around and figure it out. Keep getting either tuple index out of range or "blah" not defined.

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

template = "Hi, I'm {} and I love to eat {}!"
values = [
    {"name": "Dalton",
     "food": "pasta"},
    {"name": "Alexis",
     "food": "In N Out"}
]

def string_factory(values):
    results = []

    while item in values:
        results.append(template.format(**item))

    return results

2 Answers

Ryan S
Ryan S
27,276 Points

Hi Dalton,

You are very close. In fact it is just one keyword that is causing the issue. You need to use a for loop, not a while. Change that and it should work. for loops are used for cycling through iterables.

The other thing I should mention, although it won't affect the functionality of your code, is that you don't need to define the list of values. The challenge checker already has its own list that will be passed in to check your function.

However, if you were interested in passing in your own list (outside of this code challenge) you would need to do that when you call the function. The name of the function argument, in this case, "values", is only used in the function itself to represent the unknown list that will eventually be passed in. It will have no immediate relation to the "values" list that you defined above, even though they share the same name.

Hope this helps.

Dalton Dunn
Dalton Dunn
6,709 Points

awh of course! Thank you so much! I kept trying to input my own values and the while loop. I thought it was for, should have proofread! Thank you again!