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

Stuck on 5 Of 5

def stats(most_courses): showcase = [] for teachers,num_of_courses in my_dicts.item(): showcase.append([teachers,len(num_of_courses)])

return most_courses

Cat get past this one. seems close but keeps saying "Try again"

Any ideas? Thanks!

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(teach_dict):
    count = 0
    for item in teach_dict:
        count += 1
    return count

def num_courses(teach_dict):
    count = 0
    for arr in teach_dict.values():
        for item in arr:
            count += 1
    return count

def courses(teach_dict):
    courses = []
    for arr in teach_dict.values():
        for item in arr:
            courses.append(item)
    return courses

def stats(most_courses):
     showcase = []
    for teachers,num_of_courses in my_dicts.item():
        showcase.append([teachers,len(num_of_courses)])

    return most_courses

2 Answers

Nader Alharbi
Nader Alharbi
2,253 Points

Hello Scott,

Your logic is correct, but you made few innocent mistakes. Let's go through them.

#1) There is an unnecessary space before showcase = []
#2) In your for-loop, you should loop through the dictionary argument
# in your function which is called most_courses
#3) You are returning the original dictionary argument in your function
# you should return the showcase

Here is your function after correction

def stats(most_courses):
    showcase = []
    for teachers,num_of_courses in most_courses.items():
        showcase.append([teachers,len(num_of_courses)])

    return showcase
Steven Parker
Steven Parker
231,236 Points

The challenge instructions include "Important: In each task of this code challenge, the code you write should be added to the code from the previous task." So each task's function should remain as-is when you write the next one.

Task 4 was to create a function named "most_courses", but that function isn't there now. But curiously, that seems to be name of the parameter of "stats". Did the two functions somehow get mixed? There are some logic, syntax, and spelling issues as well, but perhaps that was caused by the mix-up.

But even a perfect "stats" function won't pass the challenge while the "most_courses" function is missing.