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

can anyone help me solve code challenge.

please any one teach me how to solve this challenge.

teachers.py
# The dictionary will be something like:
# {'Jason Seifer': ['Ruby Foundations', 'Ruby on Rails Forms', 'Technology Foundations'],
#  'Kenneth Love': ['Python Basics', 'Python Collections']}
#
# Often, it's a good idea to hold onto a max_count variable.
# Update it when you find a teacher with more classes than
# the current count. Better hold onto the teacher name somewhere
# too!
#
# Your code goes below here.
dict ={'Jason Seifer': ['Ruby Foundations', 'Ruby on Rails Forms', 'Technology Foundations'], 'Kenneth Love': ['Python Basics', 'Python Collections']}
def most_classes(dict):
    max_classes = 0
    for entry in dict:
        if len(dict[entry]) > max_classes:
            max_classes = len(dict[entry])
            max_teacher = entry
    return (max_teacher)

#Count the number of teachers in the dictionary
def num_teachers(dict):
    teacher_qty = 0
    for entry in dict:
        teacher_qty += 1
    return teacher_qty

#Now, create a function named stats that takes the teacher dictionary. Return a list of lists in the format [<teacher name>, <number of classes>]. 
#For example, one item in the list would be ['Dave McFarland', 1].
def stats(dict):
    teacher_stats = []
    for entry in dict:
        teacher_stats.append([entry,len(dict[entry])])
    return teacher_stats

#Great work! Finally, write a function named courses that takes the teachers dictionary. 
#It should return a single list of all of the courses offered by all of the teachers.
def courses(dict):
    class_list = []
    for entry in dict:
        class_list.append(", ".join(dict[entry]))
    return class_list

print(most_classes(dict))
print(num_teachers(dict))
print(stats(dict))
print(courses(dict))

1 Answer

This is what I came up with craigs

But before

dict.keys() # returns all the keys from the dict
dict.values() # returns all the values from the keys in the dict
dict.items() # returns both the keys and the values from each entity ... or key ( or whatever you wanna call it)... in the dict.
def num_teachers(teachers):
    return len(teachers.keys())

def num_courses(teachers):
    list_of_lists_of_courses_by_teacher = []
    sum_of_courses = 0
    for _ in teachers.values():
        list_of_lists_of_courses_by_teacher.append(_)
    for _ in list_of_lists_of_courses_by_teacher:
        sum_of_courses += len(_)
    return sum_of_courses

def courses(teachers):
    list_of_lists_of_courses_by_teacher = []
    c = []
    for _ in teachers.values():
        list_of_lists_of_courses_by_teacher.append(_)
    for _ in list_of_lists_of_courses_by_teacher:
        for i in _:
            c.append(i)
    return c

def most_courses(teachers):
    smartest_teacher = ''
    temp_v = 0
    # this is weird, but it passes.
    for k, v in teachers.items():
        if len(v) > temp_v:
            smartest_teacher = k
    return smartest_teacher

def stats(teachers):
    list_of_stats = []
    for k, v in teachers.items():
        list_of_stats.append([k, len(v)])
    return list_of_stats

Maybe you should not be declaring a dict by the name of dict (top of your code). The teachers dictionary is already defined for you behind the scenes by the instructor, in other words, the dict is ready to use, no need to declare one.

also, IDK if by passing a dict as a parameter you are passing an actual empty dict, better be EXPLICIT and pass the teachers dict as a param in each function.

Hope it helps!!