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

Calculate total sum of all values for a DICT

Something is missing. Could anyone have a look at my code? Thank you.

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.
import collections

def num_teachers(teacher_dict):
    return len(teacher_dict)

def num_courses(teacher_dict):
    for key, value in teacher_dict.items():
        print(key, len(list(filter(bool, value))))
        return sum(teacher_dict.values())

1 Answer

Chris Howell
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Chris Howell
Python Web Development Techdegree Graduate 49,702 Points

Hi Maiia Spivak

So your num_teachers works for Challenge 1 and looks like num_courses is failing Challenge 2 with an error message of:

Bummer! TypeError: unsupported operand type(s) for +: 'int' and 'list'

Now you aren't directly calling the operand: + but you are calling a function called sum that does.

sum(iterable) expects an iterable of usually numbers, as per Python Docs: Sum

Basically Python doesnt know how to SUM up a List of List of Strings.

def num_courses(teacher_dict):
    for key, value in teacher_dict.items():
        print(key, len(list(filter(bool, value)))) # was this a debug print?
        # during the loop at some point.....

        # when key holds: "Andrew Chalkley"
        # value holds: ['jQuery Basics', 'Node.js Basics']

        # teacher_dict.values() always return something like(below)
        # [['jQuery Basics', 'Node.js Basics'], ['Python Basics', 'Python Collections']]

        return sum(teacher_dict.values()) # sum has to be adding iterable of numbers

Does this help? Let me know :)