Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Python Collections (2016, retired 2019) Dictionaries Teacher Stats

most_courses should return the name of the teacher with the most courses.

I keep getting the wrong teacher, what am I missing.

Thanks,

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(teachers):
    count = 0
    for teacher in teachers:
        count += 1
    return count

def num_courses(teachers):
    total = 0
    for value in teachers.values():

        for course in value:
            total += 1
    return total

def courses(teachers):
    list = []
    for course in teachers.values():
        list.extend(course)

    return list

def most_courses(teachers):
    max_count = 0
    for value in teachers.values():
        for course in value:
            max_count += 1
    return max_count

1 Answer

Steven Tagawa
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Steven Tagawa
Python Development Techdegree Graduate 14,426 Points

The most_courses function that you have there has exactly the same code as the num_courses function above it, so it's doing exactly the same thing—counting up the total number of courses that are taught. If you want the name of the teacher who teaches the most courses, you need to compare the number of courses for each one. If you use a for teacher in teachers: loop, each teacher will be the value for that teacher—the list of their courses—and you can use the len() function to check how many they teach. Like, for the example that they show, len(teachers['Andrew Chalkley']) and len(teachers['Kenneth Love']) would both be 2.