Python Python Collections Dictionaries Teacher Stats

Leo Yun Tao
Leo Yun Tao
5,543 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
35,541 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
5,543 Points

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