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 (2016, retired 2019) Dictionaries Teacher Stats

The fourth task isn't going through

For the fourth function i'm supposed to print out the name of the teacher which has the most courses. I think that my code is ok, so i run it on my computer and it works, but in the challenge it says that it didn't get the right name of the teacher. Can you please help me, thank you.

teachers.py
# The dictionary will look something like:
# {'Andrew Chalkley': ['jQuery Basics', 'Node.js Basics'],
#  'Kenneth Love': ['Python Basics', 'Python Collections']}
#
# Each key will be a Teacher and the value will be a list of courses.
#
# Your code goes below here.
def num_teachers(courses):
    return len(courses.keys())
def num_courses(teachers):
    total_courses = []
    for teacher in teachers:
        one_teacher = teachers[teacher]
        total_courses = total_courses + one_teacher

    return len(total_courses)
def courses(teachers):
    courses_names = []
    for teacher in teachers:
        courses_names = courses_names + teachers[teacher]
    return courses_names
def most_courses(teachers):
    for teacher in teachers:
        if teachers[teacher] == max(teachers.values()):
            name = teacher
    return name

2 Answers

Steven Parker
Steven Parker
229,670 Points

I'm not sure what "max" will do when applied to a list of lists, maybe return the list with elements that come later in alphabetic order. But it most likely will not return the one with the most items.

You may want to use the suggestion given in the instructions about keeping a count and comparing the list length to that.

Paul Bentham
Paul Bentham
24,090 Points

The issue I had with this is: what if two teachers have the same amount of courses which happens to be the max?

As a workaround I appended them to a list and just returned the first in the list.

First off creating a list of the number of courses each had so I had something to get a max() from, then running through the list and seeing if the teachers amount of courses equals that max... if it did, appending that teacher to my list of teachers with the most courses.

def most_courses(dic1):
  courses_count = []
  most_courses = []
  for teacher in dic1:
    courses_count.append(len(dic1[teacher]))
  for teacher in dic1:
    if len(dic1[teacher]) == max(courses_count):
      most_courses.append(teacher)  

  return most_courses[0]
Steven Parker
Steven Parker
229,670 Points

Without creating a list, if you only update the saved teacher and count when the new count is higher, you'll get the same result.