Bummer! That Track couldn't be located. Perhaps one of the Tracks below is what you're looking for?

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

Struggling with 'most_classes' function in Python Collections

I'm currently working on this exercise (https://teamtreehouse.com/library/python-collections/dictionaries/teacher-stats) and am totally stumped to the point of having almost no code written.

My main issue seems to be separating the teachers from each other and then assigning them a number that represents how many classes they teach. Maybe that is the wrong approach though.

I'd really appreciate being pointed in the right direction (but not being given the answer!) or given hints as for what to try.. Or any suggestions really.

Thanks in advance!

1 Answer

Here's an outline of a simple solution:

def most_classes(teachers):
  # Declare variables to keep track of the
  # top teacher and top class count

  # For every teacher and their classes in teachers
    # If the current class count is greater than the current top class count
      # Update the top class count and top teacher

  # Return the top teacher

You may want to use the items method to get both the key and value from the dictionary.

Your original idea will work, however it will be a bit more challenging to implement (though will require less code). Here's an outline of that:

def most_classes(teachers):
  # Associate the count of the teacher's courses with their name (for example with a tuple).
  # Get the max value based on the count
  # Return the teacher of the max value

The actual solution for this method can fit on one line but it's pretty hard to read.

Thank you so much for the helpful hints, definitely going to try working with what you outlined!