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 (Retired) Dictionaries Teacher Stats

Regarding the stats function challenge in the Python Collections lesson.

My code is evaluating perfectly inside of Workspaces but does not pass the challenge and I do not see a reason why it should not. Any clues as to what is stopping my code from passing would be greatly appreciated.

teacher_stats = []
teacher_name = " "

def most_classes(teachers):
    max_count = 0

    for key in teachers:
        classes = teachers[key]
        count = len(classes)

        if count > max_count:
            max_count = count
            teacher_name = key
        else:
            continue

    return(teacher_name)

def num_teachers(teachers):
    for key in teachers:
        total_teachers = len(teachers)

    return(total_teachers)

def stats(teachers):
    for key in teachers:
        a_stat = []
        name = key
        classes = teachers[key]
        count = len(classes)
        a_stat.append("{}, {}".format(name, count))
        teacher_stats.append(a_stat)

    return(teacher_stats)

3 Answers

Josh Keenan
Josh Keenan
20,315 Points

Here's my answer:

def stats(teachers):
    a_stat = []
    for key in teachers:

        name = key
        classes = teachers[key]
        count = len(classes)
        list1 = [key, count]
        a_stat.append(list1)


    return(a_stat)

You were returning a string, you needed to return a list, but you created a global variable and ended up doing it in a complex way, keep it simple! Avoid global variables!

Thanks for showing me the way, Mr. Keenan. Thanks to your guidance I was able to pass the remaining stages in the lesson, allowing me to advance to the Tuples section. I appreciate it!

Josh Keenan
Josh Keenan
20,315 Points

What stage are you not passing? Third or fourth?

Hi Josh,
The third stage regarding the stats function is not passing.