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 (Retired) Dictionaries Teacher Stats

Jonathan Shedd
Jonathan Shedd
1,921 Points

I don't even know where to begin with this one.

The challenge is to create a function named most_classes that takes a dictionary of teachers. Each key is a teacher's name and their value is a list of classes they've taught. most_classes should return the teacher with the most classes.

Not sure how to start with this one.

teachers.py
# The dictionary will be something like:
# {'Jason Seifer': ['Ruby Foundations', 'Ruby on Rails Forms', 'Technology Foundations'],
#  'Kenneth Love': ['Python Basics', 'Python Collections']}
#
# Often, it's a good idea to hold onto a max_count variable.
# Update it when you find a teacher with more classes than
# the current count. Better hold onto the teacher name somewhere
# too!
#
# Your code goes below here.
def most_classes(**teacher):
    max_count = 0

2 Answers

This code will work, with a play-by-play of why it works. I hope that this is helpful.

def most_classes(teachers):
    # Provide empty variables
    most_classes = 0  # Value to compare against
    teacher = None  # Teacher value to return

    for teacher in teachers:  # Loop through teachers
        if len(teachers[teacher]) >= most_classes:  # Compare teacher's classes against most_classes
            most_classes = len(teachers[teacher])  # Update most_classes
            teacher = teacher  # Update teacher's name
    return teacher  # After forloop finishes, return the teacher
Martin Krkoska
Martin Krkoska
10,514 Points

Hi I am not sure it is absolutely right, but it should make a job

# create a function named most_classes hat takes a dictionary of teachers
def most_classes(dictionary_of_teachers):

# Better hold onto the teacher name somewhere and more classes too!
    teacher = ""
    most_clases = 0
    for name_of_teacher in dictionary_of_teachers:
        count_of_clases = 0
        for class_ in dictionary_of_teachers[name_of_teacher]:
            count_of_clases += 1
            if count_of_clases > most_clases:
                teacher = name_of_teacher
                most_clases = count_of_clases
    return(teacher)