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

Kathleen Rauh
Kathleen Rauh
1,785 Points

What is the expected return of the most_classes challenge?

It seems like previous questions had to do with returning a list of teachers and the number of classes taught by each. That doesn't seem to be the goal now. The challenge now says: "Create a function named most_classes that takes a dictionary of teachers and returns the teacher with the most classes."

So, I couldn't find a question/answer to help me. I am getting the "Bummer! Where's most_classes()?"

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_dict):
  max_count = 0
  for key in teacher_dict:
    class_count = len(teacher_dict[value])
    if class_count > max_count:
      max_count = class_count
      max_teacher = teacher_dict[key]
  return max_teacher

1 Answer

Martin Cornejo Saavedra
Martin Cornejo Saavedra
18,132 Points

When doing this kind of tasks, they actually ask you to program more than one functions. Don't delete the older functions in case you did.

Here's a solution in task 2:

def num_teachers(my_dict):
    return(len(my_dict))

def most_classes(my_dict):
    max_count  = 0
    for key in my_dict:
        if len(my_dict[key]) > max_count:
            max_count = len(my_dict[key])
            answer = key
    return answer