Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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,258 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