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

Strange, task 4 is no longer passing

I tested this in pycharm and it works, all previous functions still seem to work as well, but here it throws me an error saying task 4 is no longer working. Haven't touched it though.

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.
count_teachers = []

def num_teachers(teachers):
    for key in teachers:
        count_teachers.append(key)
    return len(count_teachers)

count_courses = []

def num_courses(teachers):
    values = teachers.values()
    count = 0
    for val in values:
        count += len(val)
    return count

courses_names = []
new_list = []

def courses(teachers):
    for k,v in teachers.items():
        courses_names.append(v)
    for list in courses_names:
        for item in list:
            new_list.append(item)
    return new_list

def most_courses(teachers):
    max_count = 0
    for k,v in teachers.items():
        if len(v) > max_count:
            max_count = len(v)
            max_teacher = k
        return k

final_list_teachers = []

def stats(teachers):
    for k,v in teachers.items():
        first_list = k,len(v)
        teacher = list(first_list)
        final_list_teachers.append(teacher)
    return final_list_teachers

2 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,441 Points

The return in the function most_courses is indented too far. This causes the for loop to return at the end of the first iteration.

Not flagged as an error, but the initialization of final_list_teachers should be done inside the stats() function. Otherwise, stats() would only work on the first call. Subsequently it would not be reset to the empty list.

Oh yes, now I see it, thank you very much Chris!

Please provide Chris a Best Answer. Thanks :)