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

Evandro Luis Lima Pastor
Evandro Luis Lima Pastor
1,061 Points

Print Key Error.

My code doesn't print the teacher name (key), and return a SyntaxError also.

Any idea how can I fix this?

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(teachers):
  counter=0
  for key, value in teacher.items():
    counter2=len(value)
    if counter2 < counter:
        counter = counter2
    else:
        print key

1 Answer

hie evandro. Firstly you should return hat teacher's name not print it. let me try and take you through how I understood the challenge. You did great in defining the function, initializing counter, checking for keys and values and setting counter two. Now for your if statement, we want the teacher with most classes so our counter two should always be higher than counter. NOTE I also included the variable name with an empty string in it, such that for that key that has most values I will add it to my variable. Finally return the variable name which contains the key that we are looking for .

def most_classes(teachers):

counter = 0

name = ''

for key, value in teachers.items():

    counter2 = len(value)

    if counter2 > counter:

        name = key

        counter = counter2

return name