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

Simon Tham
Simon Tham
1,555 Points

most_courses challenge. Need help to understand what is wrong

It is the most_courses challenge that I can not complete. I have tried some different solutions but I always get "Bummer: Try again". I would appreciate any tip how to proceed.

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):
    i = 0
    for key in dict:
        i += 1

    return i


def num_courses(dict):
    j = 0
    for value in dict.values():
        for courses in value:
            j += 1

    return j


def courses(dict):
    course_list = []

    for value in dict.values():
        for course in value:
            course_list.append(course)

    return course_list

def most_courses(dict):
    max_count = 0

    for value in dict.values():
        if max_count < len(value):
            max_count = len(value)

    for key, new_value in dict.items():
        if len(new_value) == max_count:
            teacher = key

    return teacher

2 Answers

Steven Parker
Steven Parker
231,269 Points

This code looks OK to me, so I copy/pasted it directly into the challenge and it passed task 4.

Not sure why it didn't work for you ... try again?

Simon Tham
Simon Tham
1,555 Points

Okey thnx for the reply. This is the second time it happened to me and I think I found the problem. I usually use tab to indent but I instead need to use 4 spaces. Then it works.

Steven Parker
Steven Parker
231,269 Points

The important thing is to be consistent with the indendation