Python Python Collections Dictionaries Teacher Stats

Mirza Gogic
Mirza Gogic
2,528 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

Frank Genova
Frank Genova
Python Web Development Techdegree Student 12,611 Points

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
Daniel Turato
Java Web Development Techdegree Student 20,075 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