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

Al Craig
Al Craig
22,220 Points

Create a function named most_courses that takes our good ol' teacher dictionary.

Struggling with this next step... Where am I going wrong?

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_teachers):
    return len(dict_teachers)

def num_courses(dict_teachers):
    courses = []
    for course in dict_teachers.values():
        courses += course
    courses = set(courses)
    return len(courses)

def courses(dict_teachers):
    courses = []
    for course in dict_teachers.values():
        courses += course
    return set(courses)

def most_courses(dict_teachers):
    max_length = 0
    longest = ''
    for key, value in dict_teachers.items():
        if max_length = 0:
            max_length = len(value)
            longest = key
        elif len(value) > max_length:
            max_length = len(value)
            longest = key
    return longest

4 Answers

Al Craig
Al Craig
22,220 Points

The most_courses function needs to return the key (teacher) with the greatest len(value) (the most courses).

Dave StSomeWhere
Dave StSomeWhere
19,870 Points

Looks like you are using an assignment operator in 1 spot where you really want to evaluate equality.

Al Craig
Al Craig
22,220 Points

This still doesn't pass. :(

def most_courses(dict_teachers):
    max_length = 0
    for key, value in dict_teachers.items():
        if max_length == 0:
            max_length = len(value)
            longest = key
        elif len(value) > max_length:
            max_length = len(value)
            longest = key
    return longest
Dave StSomeWhere
Dave StSomeWhere
19,870 Points

Passes challenge 4 of 5 for me, Did you restart the challenge and/or your browser?

Al Craig
Al Craig
22,220 Points

Passes now. I must have had something weird hidden in the code. Thanks.