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

Trevor McGill
Trevor McGill
1,409 Points

Stats challenge python

Error: "Bummer: Didn't get the right output"

I'm trying to add a list of each key/value pair into a larger list, but apparently I'm not doing that correctly.

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):
    course_list = []
    for course in dictionary.values():
        course_list += course
    return len(course_list)

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

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

def stats(dictionary):
    master_list = []
    for teacher, course_count in dictionary.items():
        return[teacher, len(dictionary.values())]

1 Answer

Steven Parker
Steven Parker
231,269 Points

If you return unconditionally from inside a loop, then the function will end during the first pass of the loop and the remaining passes will never be performed.

And an assignment or the "update" method of a dictionary might be useful in the loop body.