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

Melissa Bai
Melissa Bai
5,597 Points

Stuck in the challenge: Create a function named most_classes that takes a dictionary of teachers and returns the teacher

Don't understand the Bummer: where's 'most.classes()'

teachers.py
# The dictionary will be something like:
# 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.
 teachers = {'Jason Seifer': ['Ruby Foundations', 'Ruby on Rails Forms', 'Technology Foundations'],
  'Kenneth Love': ['Python Basics', 'Python Collections']}
def most_classes(teachers):
  max_count = ''
  classes = teachers[classkey]
  for classes in teachers.values():
    if len(classes) > max_count:
      max_count = len(classes)
  return classkey  

print most_classes

2 Answers

Dan Johnson
Dan Johnson
40,533 Points

You're using the variable classkey before it's defined which is likely why it can't call/find most_classes.

As for the function, the challenge expects the name of the teacher to be returned. What you can do is iterate through both the key and values and update the leading teacher just like you're doing with the class count, then return the leading teacher at the end.

This is my solution to this problem. It's quite simple to understand and it's efficient.

def most_classes(my_dict):
  teachers_list = {}
  for teacher in my_dict:
    teachers_list[teacher] = len(my_dict[teacher])
  return max(teachers_list, key=teachers_list.get)