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
171,345 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.