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

Jay Cruz
Jay Cruz
2,296 Points

Havent been able to figure out most_courses part . Am i completely off ?

Havent got past most_courses part of challenge, been trying about an hour lol.

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.
Treehouse_Dict = {'Andrew Chalkey': ['jquery Basics', 'Node.js Basics'], 
                  'Kenneth Love': ['Python Basics', 'Python Collections']}
def num_teachers(Treehouse_Dict):
      return int(len(Treehouse_Dict.keys())) 

def num_courses(Treehouse_Dict):
    number = 0
    for num in Treehouse_Dict.values():
        number += len(num)
    return number

def courses(Treehouse_Dict):
    course_list = []
    for courses in Treehouse_Dict.values():
        for course in courses: 
            course_list.append(course)
    return course_list


def most_courses(Treehouse_Dict):
    max_v = max(Treehouse_Dict.values())
    for teacher in Treehouse_Dict:
        if Treehouse_Dict.values() == max_v:
    return teacher

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,457 Points

You are approaching this challenge in a unique way, but let's go with it! If the challenge returns Bummer! it usually means there is a syntax error. In this case, the if statement does not have a code block to operate on. Perhaps you meant to execute the return within the if block.

After indenting the return statement, the code now runs but gives the incorrect result. The error is because the max_v value contains the maximum value of the values() as seen alphabetically! What you want is the max() of the len() of the values. Something like max_v = max(len(value) for value in Treehouse_Dict.values())

I threw in some list comprehension syntax, but you get the idea of how to get the max value. Once you have the correct max_v, the if statement needs to compare apples to apples: if len(Treehouse_Dict[teacher]) == max_v:

Post back if you need more help. Good luck!