Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Python Collections (2016, retired 2019) Dictionaries Teacher Stats

Ronald Tse
Ronald Tse
5,798 Points

I can run it and get the correct ans...

for the last part, I have tried to run it I get the correct ans. But it said I'm wrong...

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(argument):
    teacher_list = []
    for teacher in argument:
        teacher_list.append(teacher)
    return len(teacher_list)

def num_courses(argument):
    course_list = []
    for course in argument.values():
        course_list += course
    return len(course_list)

def courses(argument):
    course_list2 = []
    for course in argument.values():
        course_list2 += course
    return course_list2

def most_courses(argument):
    max_course = 0
    for teacher in argument.keys():
        if len(argument[teacher]) > max_course:
            max_course = len(argument[teacher])
    return teacher

1 Answer

Steven Parker
Steven Parker
215,958 Points

:point_right: You're very close, but missing one thing.

When you encounter a new maximum and save the number of courses for later comparisons, you should also save the teacher that number goes with. Then, after the loop finishes, return that saved teacher instead of just the last one tested in the loop.

Perhaps it appeared to work because you tested it with a list where the last teacher also happened to be the one with the most courses. Note that example data contained in challenge comments will almost never be the actual data used for testing.