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 trialLuis Bonilla II
11,481 Pointsteachers.py 2 of 5
Not sure why I'm getting TypeError: unsupported operand type(s) for +=: 'int' and 'list'
I tried this code elsewhere and it works
# 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.
def num_teachers(dict):
return len(dict)
def num_courses(dict):
courses = dict.values()
total = 0
for val in courses:
total += val
return total
4 Answers
Alex Koumparos
Python Development Techdegree Student 36,887 PointsHi Luis,
Not sure where this has been working for you. I copy-pasted it into the Python REPL and it crashed with the same error there.
The error lies in where you are updating the value of total. If you look at how you've defined courses
:
courses = dict.values()
courses
is a list containing lists (where each of the inner lists is the list of courses for a teacher).
Then when you declare your for loop:
for val in courses:
Since we know that courses
is a list of lists, and val
is defined as each element in courses
, then it is plain to see that each value of val
is itself a list. Since total
is an int (you initialised it as 0) and val
is a list, you are trying to add a list to an int, which is invalid. Hence the error message "TypeError: unsupported operand type(s) for +=: 'int' and 'list'".
Hope that clears everything up for you.
Cheers
Alex
Alex Koumparos
Python Development Techdegree Student 36,887 PointsThat is strange. I don't use Pythonista so I can't comment on why the right code wouldn't work there, but hopefully it is clear from the above why the wrong code doesn't work here.
Luis Bonilla II
11,481 PointsThis code is correct but I'm not under standing how count is adding the values?
def num_courses(str1):
count2 = 0
for value in str1.values():
for items in value:
count2 += 1
return count2
This code happens to fail in Pythonista with 'int' object not iterable
Luis Bonilla II
11,481 PointsThis is confusing for the fact the code presented up top works even in terminal but not Treehouse's interpreter
Alex Koumparos
Python Development Techdegree Student 36,887 PointsWhat do you mean by 'terminal'? Do you mean the Python REPL? If so, the code up top doesn't work in the Python REPL.
Here is a copy-paste right from Python:
>>> def num_courses(dict):
... courses = dict.values()
... total = 0
... for val in courses:
... total += val
... return total
...
>>> d = {'Andrew Chalkley': ['jQuery Basics', 'Node.js Basics'], 'Kenneth Love': ['Python Basics', 'Python Collections']}
>>> num_courses(d)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 5, in num_courses
TypeError: unsupported operand type(s) for +=: 'int' and 'list'
It crashes with exactly the same error as the Treehouse interpreter. That's because the code has the bug that I described.
Luis Bonilla II
11,481 PointsI know why, it's what I set values for before. It makes sense, my values were ints. Thanks
Ragy Abuamra
4,044 Pointstotal += len(val)
Luis Bonilla II
11,481 PointsLuis Bonilla II
11,481 PointsI ran this using Pythonista an app on iOS and the correct solution on here doesn't work on Pythonista