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

teachers.py Task 4. Why is teachers results not accepted?

For task 4 I have addressed 2 scenarios in my workspace and I have tested them out correctly.

  1. There is one teacher with most # of courses
  2. There are multiple max teachers having the same maximum number of courses.

Any ideas what I am doing 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(arg):
    num_teachers=len(arg)
    return num_teachers

def num_courses(arg):
    num_courses=0  
    for teachers in arg:
        num_courses+=(len(arg[teachers]))
    return num_courses

def courses(arg):
    list_of_courses=[]
    for teachers in arg:
        list_of_courses += arg[teachers]
    return list_of_courses

def most_courses(arg):
    most_courses_num = 0
    num_of_courses = 0
    for teacher in arg:
        num_of_courses=len(arg[teacher])
        if num_of_courses > most_courses_num:
            most_courses_teacher=[]
            most_courses_teacher.append(teacher)
            most_courses_num=num_of_courses
        elif num_of_courses == most_courses_num:
            most_courses_teacher.append(teacher)
        else:
            None
    return most_courses_teacher

after some tinkering looks like the challenge will accept an answer of 1 and only 1 teacher having the most courses. We have to code against faulty logic in order to move on.

1 Answer

Steven Parker
Steven Parker
231,236 Points

It looks like you already have an idea of what you need to change. The instructions ask for the function to return only one teacher, not a list of them.

There are no special rules given for handling a "tie", but you can assume that it will be acceptable to return any one of the top teachers if that should happen.