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

Lost on 4th Task of teachers.py

Not sure what I am doing wrong on most_courses function. I've been working on it for a while and I can not figure it out. The first 3 tasks for fairly straightforward, but I know I am missing something on this task. I just can not see what it is. The message I keep receiving is: Bummer! Didn't get the right teacher name!

def most_courses(dictionary):
    max_count = 0
    for teacher, value in dictionary.items():
        if len(value) > max_count:
            max_count = len(value)
            name = teacher
        return str(name)
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(dictionary):
    return len(dictionary)

def num_courses(dictionary):
    total_list = []
    for value in dictionary.values():
        total_list += value
    return len(total_list)

def courses(dictionary):
    single_list = []
    for value in dictionary.values():
        single_list += value
    return single_list

def most_courses(dictionary):
    max_count = 0
    for teacher, course in dictionary.items():
        if len(course) > max_count:
            max_count = len(course)
            name = teacher
        return name

[MOD: corrected formatting -cf]

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,441 Points

Hey John Davis! You are VERY close. Due to the indent on the return statement, it is inside the for loop and is executed on the first iteration through the loop. This makes it return the count of whatever teacher happens to be seen first. To correct this, unindent the return so that it is aligned with the for statement so it only executes after the for loop completes.

Post back if you have more questions. Good luck!!

That did it! Thank you very much!