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

Shadow Skillz
Shadow Skillz
3,020 Points

Task 4

Not sure how to get the teachers with the most classes.

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

def num_courses(dic):
    course = 0
    for teachers in dic:
        course += len(dic[teachers])
    return course    

def courses(dic):
    class1 = []
    for classes in dic.values():
        class1.extend(classes)
    return class1  

def most_courses(dic):
    max_count = 0
    teachers = " "
    for teach, m_courses in dic.iterems():
        if m_courses > (max_count):
            max_count = len(m_courses)
        teachers = teach
    return teachers    

2 Answers

james south
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
james south
Front End Web Development Techdegree Graduate 33,271 Points

you are close, just need to tweak a few things. in your loop, the (a) proper dict method is items, not iterems. then in your if statement, you want to compare the length of the values to max, not the values themselves. you are updating max correctly, but the next line is indented wrong. tab it over right once to be under the max_count = len(m_courses) line, so the teacher to be returned is only updated if max is also updated. otherwise the return value is simply the last teacher in the loop. after these changes i think it will work. edited after passing to show while iteritems is a dict method, the challenge will throw an error, so use items.

Shadow Skillz
Shadow Skillz
3,020 Points

thanks for the reply James but I'm still having issues. It's telling me (TypeError: unorderable types: str() > int()) can you provide some visual assistance i would appreciate it.

Gabor Kovacs
Gabor Kovacs
4,874 Points
def most_courses(ts):
    for t,c in ts.items():
        if max(c):
            return t