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

Gabriel McGraw
Gabriel McGraw
2,317 Points

Code is not passing correctly in treehouse, I've tested it in an IDE and it works!

The function that's not passing is "def most_courses(my_dict):".

teachers.py
# The dictionary will look something like:
my_dict = {'Andrew Chalkley': ['jQuery Basics', 'Node.js Basics','example1','example2'],
  '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(my_dict):
    lst = []
    for k,v in my_dict.items():
        lst.append(k)
    return len(lst)

def num_courses(my_dict):
    lst = []
    for k,v in my_dict.items():
        for i in v:
            lst.append(i)lst = []
    for k,v in my_dict.items():
        return max(numcourses(my_dict))
    return len(lst)

def courses(my_dict):
    lst = []
    for k,v in my_dict.items():
        for i in v:
            lst.append(i)
    return lst

def most_courses(my_dict):
    lst = []
    for k,v in my_dict.items():
        lst.append(len(v))
    most = max(lst)

    for k,v in my_dict.items():
        if len(v) == most:
            return k

4 Answers

Hey Gabriel, try this:

def most_courses(x):
    teacher, max_count = "", 0
    for key, value in x.items():
        count = 0
        for i in value:
            count += 1
        if count > max_count:
            teacher, max_count = key, count
    return teacher

If you have any questions, I will edit my answer accordingly. If you do not have any questions, remember to upvote and choose best answer so that this question receives a checkmark in forums.

Gabriel McGraw
Gabriel McGraw
2,317 Points

It's not passing through either. Your solution does work in an IDE though.

Could you elaborate, please? What is the error message displaying.

Replace your code with this code:

def num_teachers(x):
    num = 0
    for i in x:
        num += 1
    return num


def num_courses(x):
    num = 0
    for key, value in x.items():
        for i in value:
            num += 1
    return num


def courses(x):
    li = []
    for key, value in x.items():
        for i in value:
            li.append(i)
    return (li)


def most_courses(x):
    teacher, max_count = "", 0
    for key, value in x.items():
        count = 0
        for i in value:
            count += 1
        if count > max_count:
            teacher, max_count = key, count
    return teacher


def stats(x):
    li = []
    for key, value in x.items():
        count = 0
        for i in value:
            count += 1
        li.append([key, count])
        count = 0
    return li

Remember to upvote and choose best answer so that your question receives a checkmark in forums.

Gabriel McGraw
Gabriel McGraw
2,317 Points

It just says "Bummer, try again" when I run it, there's no actual error.

I assume you uncommented my_dict?

Recomment it by putting # on lines 2 and 3:

# The dictionary will look something like:
# my_dict = {'Andrew Chalkley': ['jQuery Basics', 'Node.js Basics','example1','example2'],
# '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.
Gabriel McGraw
Gabriel McGraw
2,317 Points

Okay I figured it out, when I clicked next on the previous steps it changed my code. Thanks for your help though.

William Warren
William Warren
2,519 Points

i'm having the same issue

any ideas?

'''python def most_courses(t_dict): max_teach = " " max = 0 count = 0 info = t_dict.values() names = t_dict.keys() for i in info: if max < len(i): max = len(i) max_teach = names[count] count += 1 else: count += 1 return max_teach '''

William Warren
William Warren
2,519 Points

I'm getting a Type Error which reads "Dict_keys" object does not support indexing, yet it works in an online code fiddle.

William Warren Your code isn't formatted correctly, could you change it please and I'll take a look at it?