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

Frank Sherlotti
PLUS
Frank Sherlotti
Courses Plus Student 1,952 Points

Teacher stats: Challenge 1 need some help please.

Create a function named most_classes that takes a dictionary of teachers and returns the teacher with the most classes: I understand it wants me to hold a maxcount variable along with a place for the teacher with the most classes name. But where I get completely lost is when I have to write out loops. For some reason I just can't figure out how they work or how there suppose to be written out. I know something has to go in between the 'for' and 'in' but I don't understand what is supposed to be there. Please any help would be greatly appreciated, thanks!

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(a_dict):
  maxcount = 0
  teacher = ''
  for  in a_dict:

5 Answers

Jeremy Fisk
Jeremy Fisk
7,768 Points

There is a function for dictionaries entitled items() that allows you to call on the keys and values,

so your for loop can look like this:

for key, value in a_dict.items():

1) this will allow you to find the length of the number of classes for each teacher (i.e. the values of a_dict)

2) then you can compare that length to the max_count variable, if that length is greater than your max_count

3) the key becomes your teacher with the most classes and your max_count becomes the length of your value

4) finally, return your key value for the teacher with the most classes

Frank Sherlotti
PLUS
Frank Sherlotti
Courses Plus Student 1,952 Points

I'm now stuck here if i'm even doing it right. I don't quite understand what i'm comparing to maxcount. It all just doesn't make sense.

def most_classes(a_dict):
  maxcount = 0
  teacher = ''
  for key, value in a_dict.items():
    if:

  return teacher
Jeremy Fisk
Jeremy Fisk
7,768 Points

reference earlier discussion #1) after your for loop, find the length of the number of classes for each teacher (i.e. the values of a_dict):

teacher_value = len(value)

Then, proceed to your if statement:

if teacher_value > max_count:

then your teacher variable = key

and max_count= teacher_value

Frank Sherlotti
Frank Sherlotti
Courses Plus Student 1,952 Points

Getting a where's 'most_classes()' error now.

def most_classes(a_dict):
  maxcount = 0
  teacher = ''
  for key, value in a_dict.items():
    teacher_value = len(value)
    if teacher_value > max_count:
      max_count = teacher_value
  return teacher
Jeremy Fisk
Jeremy Fisk
7,768 Points

looks good, you initiated the teacher variable at the beginning but now you must allocate information to teacher in order to return something useful, you need to assign a value to teacher, add teacher = key just prior to your return statement:

so, in a_dict you have key: value pairs, and each pair has the teacher name as the key, and the list of classes that the teacher teaches are the value associated with that key.

Frank Sherlotti
Frank Sherlotti
Courses Plus Student 1,952 Points

Same error unless I put the teacher = key in the wrong spot.

def most_classes(a_dict):
  maxcount = 0
  teacher = ''
  for key, value in a_dict.items():
    teacher_value = len(value)
    if teacher_value > max_count:
      max_count = teacher_value
      teacher = key
  return teacher
Jeremy Fisk
Jeremy Fisk
7,768 Points

also, you initiated the variable maxcount with no underscore:

maxcount=0

but then, at the end, you change the variable to include an underscore:

max_count = teacher_value
Jeremy Fisk
Jeremy Fisk
7,768 Points

no problem, i have been helped a lot on the forum, and helping others helps me to learn too! good luck man