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

This code works on my python notebook. using the same arguments passed. It doent pass in the treehouse console. Help

Additionally the same code worked the first time i tried it. Don't know whats the issue here.

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.
def most_classes(dicts):
  my_dicts={}
  max_count=0
  y=0
  teacher=''
  for key in dicts:
        for value in dicts.values():
            for x in value:
                y=y+1
        if max_count<y:
            max_count=y
            teacher=key
        else:
            continue
  return teacher

1 Answer

Gavin Ralston
Gavin Ralston
28,770 Points

I'm going to rewrite some of the variable names here to see if it makes it a little easier to see what you're doing...and commenting on the lines as I go

def most_courses(teachers):
  my_dicts= {}        # nothing is done with this variable, it's safe to remove
  max_count = 0      # presumably to get updated with the largest count (see below)
  course_count = 0  # you're using this to count classes for each teacher, I think
  top_teacher = ''    # holding the teacher name who should be the "most class having" I think

  # good naming convention to keep things straight is "for *singular* in *plural*" 
  for teacher in teachers: 
        # see how you're grabbing course lists for each teacher here?
        for courses in teachers.values():
            # then counting the courses?
            for course in courses:
                # then adding to the course_count total?
                course_count = course_count + 1
            # but you don't reset that course count when you're done using it?

        if max_count < course_count:  # always true as long as you have more courses in the overall list of teachers
            max_count = course_count  # so this is always changed unless a teacher has no courses
            top_teacher = teacher        # so this will most likely wind up being the last teacher in the list
        else:
            # unnecessary, but I'm guessing the idea here is if there are 
            # zero courses you just go back to the top of the for loop
            continue 

  return teacher  # returns the last teacher in your list that had courses

Contacts are blurry and I haven't run this, but this might illustrate why variable names are really important, even in smaller scripts, to help you keep things straight when you're dealing with logic errors (which won't get stopped like a syntax error will)

Gavin Ralston
Gavin Ralston
28,770 Points

I don't suggest commenting like that on a regular basis, but I figure maybe the step by step commenting might help to follow along in this case. :)