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

Alex Rendon
Alex Rendon
7,498 Points

How do I know which is the teacher with most classes?

I don't understand how to put the teacher with most classes. Help me please.

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.

1 Answer

Vittorio Somaschini
Vittorio Somaschini
33,371 Points

Hello Alex.

what we want to do here is to loop through the dictionary of teachers. Each teacher (the key of the dictionary) has a list of courses (values) associated to him.

So, for each teacher we can check the number of values associated, the beginning of the code is going to be something like this:

def most_classes(dictionary):
  count = 0
  busy_teacher = ""
  for teacher in dictionary:
    if len(dictionary[teacher]) > count:

Let's analyze the code I wrote: first line -> function definition as usual second line -> we set a variable count equal to 0 (we will use this to see what's the top count of courses. third line -> we create an empty variable that will then store the name of the teacher with the most courses.

The for loop, goes through all the teachers of the dictionary, and IF the number of courses is more than the value of the variable count, it will need to run some other code that still needs to be filled in (I leave to you).

So, you need to set the count and the busy_teacher in the IF case and at the end, the function has to return the busy_teacher.

Let me know if you need more help.

Vittorio