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

Leo Yun Tao
Leo Yun Tao
15,956 Points

Help! I am stuck in the last Collections Challenge Task 4 of 5

I have only managed to find the length of the courses in the dictionary but I don't know what to do next

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):
    teachers = 0
    for teacher in dictionary.keys():
        teachers += 1
    return teachers


def num_courses(dictionary):
    courses = 0
    for course in dictionary.values():
        for cours in course:
            courses += 1
    return courses

def courses(dictionary):
    array = []
    for course in dictionary.values():
        array += course
    return array

def most_courses(dictionary):
    array = []
    for teacher in dictionary:       
        array.append(len(dictionary[teacher]))
        array.sort()

1 Answer

Cheo R
Cheo R
37,150 Points

Whenever I get stuck, I like to write the prompt as comments and fill in what I can, then investigate what I don't. So in your case:

# Task 4 of 5

# {
#     'Andrew Chalkley': ['jQuery Basics', 'Node.js Basics'],
#     'Kenneth Love': ['Python Basics', 'Python Collections']
# }

# Create a function named most_courses
#     that takes our good ol' teacher dictionary.
def most_courses(dictionary):

# You might need to hold onto some sort of max count variable.
    current_teacher_with_the_most_courses_is = None
    current_max_courses_is = None

# I know I have to cycle through the dictionary
#    get the name of teacher
#    compare the number of classes they teach to current_max_courses_is

#    if there is a new max courses:
#         update current_max_courses_is to reflect the new max courses count
#         update current_teacher_with_the_most_courses_is to reflect the new teacher with the max courses count

# most_courses should return the
#  name of the teacher with the most courses.
    return teacher_name_with_most_courses

Your code is correctly iterating through the teacher names in the dictionary, and correctly checking the number of courses for each teacher. Instead of adding the courses length to an array, do a comparison against your max count variable, update both your max count variable and the name of the teacher that currently has the most courses.

Leo Yun Tao
Leo Yun Tao
15,956 Points

Thanks for helping me :) now i can finally solve the problem