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

thanh trung
thanh trung
4,246 Points

im having trouble with code challege task 1 of 4

i dont know where to start? can anyone please help?

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 somewher
# too!
#
# Your code goes below here.

2 Answers

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

You need to go through all of the teachers (keys) and their lists of courses (values) and find the teacher w/ the most courses. How would you start that?

Juan Martin
Juan Martin
14,335 Points

Hello my friend :)

You have to loop through the keys and values of the dictionary using ".items()". Here's a solution of how you can do this:

def most_classes(teachers_dict):
  max_count = None # initializing max_count
  for teacher, courses in teachers_dict.items():
    if max_count is None: # we use this to take the first values as a starting point in order to compare later
      max_count = len(courses) # the first courses' number is set as the max value
      max_teacher = teacher # the courses' teacher
    elif len(courses) > max_count: # if the other courses' number is greater than the one saved before, let's update the number and teacher
      max_count = len(courses) 
      max_teacher = teacher
  return max_teacher

Hope this helps!