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

Mirza Gogic
Mirza Gogic
4,342 Points

Function named most_courses that takes a dictionary and returns the name of the teacher with most courses.

I am not sure why I get the response: Couldn't find most_courses. Sometimes I also get a general Bummer message.

I am practicing on another computer (on REPL.it), and the code returns the teacher with the most courses. What am I doing 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(diction):
    keys_num = diction.keys()
    index = 0
    for i in keys_num:
        index += 1
    return index

def num_courses(diction):
    cours_num = diction.values()
    index = 0
    for i in cours_num:
        for n in i:
            index += 1
    return index

def courses(diction):
    courses_names = diction.values()
    kaka = []
    for i in courses_names:
        for n in i:
            kaka.append(n)
    return kaka

def most_courses(diction):
    max_count = max(len(value) for value in diction.values())
    for k, value in diction.items():
        if len(value) == maxcount:
            return k

2 Answers

Your code:

def most_courses(diction):
    max_count = max(len(value) for value in diction.values())  # calling it max_count here with underscore
    for k, value in diction.items():
        if len(value) == maxcount: # calling it maxcount here with no underscore
            return k

You assign the variable max_count (with an underscore in the variable) and then later in your comparison statement, you are seeing if it equals maxcount with no underscore. Keep the variable names consistent and re-run the code.

You have not included created diction. If that step is missing, try adding that.

Daniel Turato
seal-mask
PLUS
.a{fill-rule:evenodd;}techdegree seal-36
Daniel Turato
Java Web Development Techdegree Graduate 30,124 Points

This is what I got for the challenge. You misspelt max_count in if the if statement

def num_teachers(dict):
    num = 0
    for keys in dict.keys():
        num += 1
    return num

def num_courses(dict):
    num = 0
    for values in dict.values():
        for c in values:
            num += 1
    return num

def courses(dict):
    courses = []
    for values in dict.values():
        for c in values:
            courses.append(c)
    return courses

def most_courses(dict):
    max = 0
    teacher = ""
    for key, value in dict.items():
        temp = 0
        for c in value:
            temp += 1
        if temp > max:
            max = temp
            teacher = key
    return teacher

def stats(dict):
    list = []
    for key, value in dict.items():
        courses = 0
        for c in value:
            courses += 1
        list.append([key, courses])
    return list