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

My code get exactly output, but couldn't pass the quiz

Here is my code

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(dict_teacher):
    return len(dict_teacher.keys())

def num_courses(dict_teacher):
    length = []
    for value in dict_teacher.values():
        length.extend(value)
    return len(length)

def courses(dict_teacher):
    length = []
    for value in dict_teacher.values():
        length.extend(value)
    return length

def most_courses (dict_teacher):
    mos_tea = ""
    max_courses = 0
    for teacher in dict_teacher.keys():
        if len(dict_teacher[teacher]) > max_courses:
            max_couses = len(dict_teacher[teacher])
            mos_tea = teacher
    return mos_tea

    def stats (dict_teacher):
    lists=[]
    for teacher in dict_teacher:
        ini_list = [teacher,len(dict_teacher[teacher])]
        lists.append(ini_list)
    return lists
Philip Schultz
Philip Schultz
11,437 Points

I think I had this same issue.....what happens if you declare the list ('ini_list') at the top of the for loop?

def stats (dict_teacher):
    lists=[]
    for teacher in dict_teacher:
        ini_list = []
        ini_list = [teacher,len(dict_teacher[teacher])]
        lists.append(ini_list)
    return lists

It would be helpful to be able to read the challenge without having to work through the first steps on my own. Could you paste the text of the challenge you're stuck on?

Without knowing what is stuck I would say num_courses could be refactored.

1 Answer

Philip Schultz
Philip Schultz
11,437 Points

Hey, I think your indentation is off on the last function.