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

The fourth question of this code challenge works in workspaces but not in the code challenge... what's wrong?

I realize that this code is a bit clunky, but I still don't understand why it won't work in the code challenge. I would highly appreciate it if someone knew what I am doing wrong.

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

def num_courses(dictionary):
    num = 0
    for value in dictionary.values():
        num += len(value)
    return num

def courses(dictionary):
    courses = []
    for value in dictionary.values():
        courses += value
    return courses

def most_courses(dictdef most_courses(dictionary):
    new_courses = {n: len(dictionary[n]) for n in dictionary.keys()}
    return list(new_courses.keys())[list(new_courses.values()).index(max(new_courses.values()))]

1 Answer

Steven Parker
Steven Parker
216,136 Points

You say this works in the workspace? Those last 3 lines don't look at all like Python syntax to me, the "def" line doesn't even have balanced parentheses. Look closely, maybe something didn't get pasted right.

def most_courses(dictionary): new_courses = {n: len(dictionary[n]) for n in dictionary.keys()} return list(new_courses.values()).index(max(new_courses.values()))]

This code worked great! You're right, the "def" line did not get printed right. Thanks!