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

Code Challenge: Teacher stats (Task 4/5)

"Wow, I just can't stump you! OK, two more to go. I think this one's my favorite, though.

Create a function named most_courses that takes our good ol' teacher dictionary.

most_courses should return the name of the teacher with the most courses. You might need to hold onto some sort of max count variable."

I get "Bummer: Try again!". Not really sure why. I've ran code on python platform an it works, so not sure why i'm getting error here :(

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(argum):
    count = 0
    for key in argum.keys():
        count += 1
    return count

def num_courses(argum):
    count = 0
    for value in argum.values():
        for valu in value:
            count += 1
    return count

def courses(argum):
    lista = []
    for value in argum.values():
        for valu in value:
            lista.append(valu)
    return lista

def most_courses(argum):
    max_count = 0  #reset max counter
    name = ""  #set default name value
    for key in argum.keys():   #go through each key and reset counter 
        count = 0
        for value in argum[key]:   #go through each key's value and count all values
            count += 1
        if count > max_count:   #could be >= if we exclude elif from below and just pick up last teacher with same max_count as some previous teacher
            max_count = count
            name = key
#        elif count == max_count:  Add these lines if we want to avoid no max_count(people having same max_count)
#            name = None
    return name

1 Answer

Anthony Crespo
seal-mask
.a{fill-rule:evenodd;}techdegree
Anthony Crespo
Python Web Development Techdegree Student 12,973 Points

From a list like this:

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

You want this:

[["Kenneth Love", 5], ["Craig Dennis", 10]]

def stats(arg):
    final_list = []
    for key, value in arg.items():  # items() return the keys and values in a tuple
        # append a list containing the teacher name and the number of courses
        # example: ["Kenneth Love", 5]
        final_list.append([key,len(value)])
    return final_list

Are you sure about that? Can you check the question again? It only asks for a name i think. Or am i missing something?