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 Teacher Stats

Luis Bonilla II
Luis Bonilla II
11,481 Points

teachers.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

teachers.py
# 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
seal-mask
.a{fill-rule:evenodd;}techdegree
Alex Koumparos
Python Development Techdegree Student 36,887 Points

Hi 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

Luis Bonilla II
Luis Bonilla II
11,481 Points

I ran this using Pythonista an app on iOS and the correct solution on here doesn't work on Pythonista

Alex Koumparos
seal-mask
.a{fill-rule:evenodd;}techdegree
Alex Koumparos
Python Development Techdegree Student 36,887 Points

That 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
Luis Bonilla II
11,481 Points

This 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
Luis Bonilla II
11,481 Points

This is confusing for the fact the code presented up top works even in terminal but not Treehouse's interpreter

Alex Koumparos
seal-mask
.a{fill-rule:evenodd;}techdegree
Alex Koumparos
Python Development Techdegree Student 36,887 Points

What 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
Luis Bonilla II
11,481 Points

I know why, it's what I set values for before. It makes sense, my values were ints. Thanks