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

RM Hrdr
RM Hrdr
3,491 Points

The coding challenge for teacher stats: task 4 of 5

Hello, fellow Pythonistas!

I've grappled with this task spending way too much time and should have applied the 15-minute rule to tackle this piece of the challenge. The issue is I'm still receiving the error Bummer: Didn't get the right teacher name despite having the code working in REPL. Also, when I included only a single return teacher function there is no value returned when I hit to execute most_courses but when I include another return in the main for loop I get a value back, how did this occur?

Thank you in advance for your inputs!

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.

teachers_list = {'Andrew Chalkley': ['jQuery Basics', 'Node.js Basics'], 
                 'Kenneth Love': ['Python Basics', 'Python Collections']}


def num_teachers(teachers_list):
     return len(teachers_list)

def num_courses(teachers_list):
    count = 0
    for values in teachers_list.values():
        for course in values:
            count += 1
    return count

def courses(teachers_list):
    courseList = []
    for values in teachers_list.values():
        for course in values:
            courseList.append(course)
    return courseList

def most_courses(teachers_list):
    max_courses = [max(len(value) for value in teachers_list.items())]
    for teacher,courses in teachers_list.items():
        for course in courses:
            if len(course) == max_courses:
                return teacher
    return teacher

**In REPL**

>>>
Blackbox:coding-challenges rmph$ python -i teachers-solution.py
>>> most_courses(teachers_list)
'Kenneth Love'
>>>
RM Hrdr
RM Hrdr
3,491 Points

@Jasper Maposa, that worked! didn't even noticed it. Appreciate your help.

1 Answer

Remove the hard-coded teachers_list to allow passing in of any dictionary when your code is being tested.