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

Ian Cruz
Ian Cruz
4,186 Points

TeacherStat challenge. I dont understand what to do from here

this question is sizzling my brain, i dont understand what i am supposed to do and how im supposed to write it.guaaah

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.
teacher = {'Jason Seifer': ['Ruby Foundations', 'Ruby on Rails Forms', 'Technology Foundations'],
         'Kenneth Love': ['Python Basics', 'Python Collections']}

def most_classes(teacher):
  most_classes = 0
  for keys in teacher.values:

1 Answer

Hi Ian

This is how i did it.

# 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.

# first way of doing it

def most_classes(mydic):
  max_classes =None # string to hold the max count of classes 
  teacherWithMaxClasses=None # this string will hold the teacher with  maximum classes
  classes=[] # i use this list as a holder for the list of teachers classes, this then helps me use the max function to get the list with the maximum classes in it
  for value in mydic.values():
      classes.append(len(value))
  max_classes=max(classes)
  for key,value in mydic.items():   # I then compare if the length of each list to the max_classes variable and if they are equal that means that key and value must be the teacher with the maximum classes, i then add key aka the name to the teacherWithMaxClasses variable.
    if len(value)==max_classes:
      teacherWithMaxClasses=key
  return  teacherWithMaxClasses

# second way of doing it 

def most_classes(mydic):
    newdict = {}
    for key, value in mydic.items():
        newdict[len(value)]= key # swap the values for keys
    return newdict[max(newdict.keys())] # then call the max function passing in the dictionary keys which will return the key aka the value with the maximum count

hope this helps

Ian Cruz
Ian Cruz
4,186 Points

Hey thanks your help, i tried implementing your code and i even tried straight up copying and pasting it in but it's not working. it keeps saying "Bummer! most_classes()returned {'Jason Seifer': ['Ruby Foundations', 'Ruby on Rails Forms', 'Technology Foundations']}, expected 'Jason Seifer'. Sorry if im missing something simple, im still trying to figure out the logic in programming through my brain haha but thanks again for your help

Hi Ian

Apologies I ran the code locally and it worked, but forgot to check what the challenge was asking. Try my code now and it should pass. Basically my code was returning a dictionary of the teacher with the maximum classes rather than just the teachers name, hence the challenge did not pass. In terms of programming logic it comes with practice, also try to come up with the solution in more of a practical sense, once you have found that practical solution then try putting it into code. It also helps to pay more attention to the problem at hand rather then jumping to the solution.

hope this helps