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

Theo Allez
Theo Allez
7,879 Points

Hi, I keep getting an error saying it cant find 'most_courses'.

Hi, I keep getting and error saying it cant find 'most_courses' in my code. Could you please help me to fix the error. Thanks

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):
    number_of_teachers = 0
    for key in dictionary:
        number_of_teachers += 1
    return number_of_teachers

def num_courses(dictionary):
    number_of_courses = 0
    courses = []
    for key in dictionary:
        for value in dictionary[key]:
            if value in courses:
                continue
            else:
                number_of_courses +=1
    return number_of_courses

def courses(dictionary):
    courses = []
    for key in dictionary:
        for value in dictionary[key]:
            if value in courses:
                continue
            else:
                courses = courses + [value]
    return courses

def most_courses(dictionary):
    teacher_dict = {}
    for key in dictionary:
        teacher_dict[key] = 0
        for value in dictionary:
            teacher_dict[key] += 1
    teacher = 0
    teacher_name = ''
    for key in teacher_dict:
        if teacher_dict[key] > teacher:
            teacher = teacher_dcit[key]
            teacher_name = key
    return teacher_name

3 Answers

Stuart Wright
Stuart Wright
41,120 Points

Very simple answer: you have a typo on the third last line of your program!

            teacher = teacher_dcit[key] . # should be teacher_dict
                               ^^
Ignazio Calo
PLUS
Ignazio Calo
Courses Plus Student 1,819 Points

I'm sorry for you, the error message was really unclear. The function most_courses is definitely there :) The error is caused because you've a typo on your code teacher_dcit

Additionally I would suggest you to think again about what's inside the function because there are few mistake and the code can be rewritten in a more easy way.

One problem is that you count the number of courses with this line:

for value in dictionary:
    teacher_dict[key] += 1

but you don't wanna loop over dictionary but I think you should iterate over dictionary[key]

anyway...you don't need to so many complex thing, I don't wanna write you the final solution, so I will write here in pseudocode:

start with an empty teacher name and a "max_course_counter = 0". Loop through the dictionary and if the len(<array of courses>) is greater than "max_course_couter", save the name of the teacher and update the "max_course_counter". At the end return the name of the teacher. In this way you have a very clean code and it's also more efficient because you loop only once.

If you have any other questions, please reply to this post.

Theo Allez
Theo Allez
7,879 Points

Thank you both for you answers, I kept seeing the error and I didn't even think about checking the rest of the code for typos :) Also cheers Ignazio for showing a more streamlined way to solve the problem, I knew my way was inefficient so seeing a better way is helpful.