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

Teachers dict challenge

It just looks like my code should work. It is saying that I am returning nothing, as if it is returning the value of most_teach = "" without ever updating it. This suggests my if-statement isn't even running.

I saw some answers that use items() in the for-loop but I really think this challenge should be solve-able without calling items. Any help would be greatly appreciated!

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(my_dict):
  #hold onto the count variable
  max_count = 0
  #hold onto the teacher name
  most_teach = ""
  #turn the teacher name keys into a list of teacher names
  teachers = list(my_dict.keys())
  #loop through the dict using each name from the list
  for teacher in teachers:
    #turn the result into an number of classes
    classes = len(my_dict[teacher])
    #if that number is higher than the current value of max_count then replace it
    if classes > max_count:
      classes = max_count
      #and store the name of the teacher, or key
      teacher = most_teach
      #print out the name of the teacher
  return most_teach

1 Answer

Erick Kusnadi
Erick Kusnadi
21,615 Points

Almost there man, small bug:

teacher = most_teach

should be:

most_teach = teacher

Because of that most_teach never changes, and since it was initialized with "", it always returns ""

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Same for classes = max_count. Should be max_count = classes

Oh geez. Thanks guys, I knew I had to be close!