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

Tomme Lang
Tomme Lang
1,118 Points

Trouble with teachers.py task 4 of 5

I really want to use count/max for this, but cant figure out where to put it. I think I am making this much harder than it has to be. I've included task 1 through 3 incase there is something in there that has tripped me up.

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(dict):
    return len(dict)

def num_courses(dict):
    list = []
    for v in dict.values():
        list += v
    return len(list)

def courses(dict):
    new_list = []
    for v in dict.values():
        for v in v:
            new_list.append(v)
    return new_list

def most_courses(dict):
    most_list = []
    for k in dict():
        for max_courses in dict[k]:
            most_list.append(max_courses)
    return (most_list)

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

The missing piece is how to

  • track the maximum number of courses seen so far
  • compare this maximum to the current teachers course count
  • if a new maximum is found,
    • save the name of the teacher
    • save the new maximum

You can get the number of courses for the current teacher using len(max_courses).

Remember to initialize the current maximum course count at 0 before the loop to compare courses.

Be sure to return the name of the teacher with the maximum course count.

Post back if you need more help. Good luck!!

Tomme Lang
Tomme Lang
1,118 Points

Got it, finally. Thanks. That one had me stumped