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

Agnes Sharan
PLUS
Agnes Sharan
Courses Plus Student 4,078 Points

'int' object is not iterable

I got an error stating 'int' object is not iterable in this program. I can't find where I have tried to iterate 'int' in my code. If I have, how can I change the code to remove the error?

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 {name} and I love to eat {food}!"

def unpacker(num, name, food):
  if num == 0:
    return name
  else:
    return food

def string_factory(values):
    string_list = []
    for i in len(values): 
        string_list = string_list.append(template.format(name = unpacker(0, **values[i-1]), food = unpacker(1, **values[i-1])))
    return string_list

1 Answer

james south
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
james south
Front End Web Development Techdegree Graduate 33,271 Points

this line:

for i in len(values): 

is causing that error. len(values) returns an int so it is not iterable. the range method takes an int and seems to be what you meant: for i in range(4) will iterate through i = 0,1,2,3. when you make this change, another error will be thrown on the next line. to use the append method to add to a list, you do not need to reassign the list to itself, simply call the append method with an argument, like myList.append(myValue).