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

Rodrigue Loredon
Rodrigue Loredon
1,338 Points

Please help with the teachers.py script

This is the challenge: create a function named most_classes that takes a dictionary of teachers and returns the teacher with the most classes.

My code is not working I always get the wrong teacher.

I've been on this for 24 hours, I'm close to discouragement :(

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

def most_classes(teachers):
  max_count = 0
  course_number = 0
  best_teacher =''

  for teacher in teachers:
        for courses in teachers.values():
            course_number = len(courses)

        if max_count < course_number:
            max_count = course_number
            best_teacher = teacher

  return best_teacher

I think the code best_teacher is not clarified enough.

Rodrigue Loredon
Rodrigue Loredon
1,338 Points

Sorry about that, best_teacher is the teacher with most courses.

2 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,441 Points

Hi Rodrigue, this issue with your nested loop is the value of course_number will retain the value from the last loop.

for courses in teachers.values():
    course_number = len(courses)

Instead you can get the current course number value for this teacher using the outer-loop variable teacher:

course_number = len(teachers[teacher])
Rodrigue Loredon
Rodrigue Loredon
1,338 Points

Exactly what I figured out with the help of the work around you've shown me, thank you again.