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 trialRyo Yamamoto
9,165 PointsCan't insert the number of courses because of "TypeError: 'int' object is not iterable" error
I wrote this code, but since I used this below:
individual_list.extend(str(len(dictionary[course])))
the number of the courses is strings not integers, and I'm getting this error "TypeError: 'int' object is not iterable" if I take out str(). Does anyone suggest any way to modify this?
# The dictionary will look something like:
# {'Andrew Chalkley': ['jQuery Basics', 'Node.js Basics'],
# 'Kenneth Love': ['Python Basics', 'Python Collections']}
#
# Each key will be a Teacher and the value will be a list of courses.
#
# Your code goes below here.
{'Kenneth Love': ['Python Basics', 'Python Collections'], 'Andrew Chalkley': ['jQuery Basics', 'Node.js Basics'], 'Ryo': ['English', 'Geography', 'PE']}
def stats(dictionary):
course_list = []
for course in dictionary:
individual_list = course.split(',')
individual_list.extend(str(len(dictionary[course])))
course_list.append(individual_list)
return course_list
2 Answers
Ryo Yamamoto
9,165 PointsThanks for your comment, but just replacing "extend" with "append" solved this..
def stats(dictionary):
course_list = []
for course in dictionary:
individual_list = course.split(',')
individual_list.append(len(dictionary[course]))
course_list.append(individual_list)
return course_list
Krishna Pratap Chouhan
15,203 PointsWhat error is trying to tell us is that you are doing something similar to:
for i in 2:
print(i)
But what you meant would be something like:
for i in range(2):
print(i)
range() function is what you should be using.
Also, remember that...
for i in "HelloWorld"
print i
would work without a doubt but cannot iterate through a integer.
Comment further if this doesn't solves your problem. (y)