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

Juan Davin
Juan Davin
2,792 Points

python

I need help regarding this code. can't find anything wrong but it won't work

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(dic):
    return len(dic)

def num_courses(dic):
    course=[]
    for value in dic.values():
        course+=value
    return len(course)    

def courses(dic):
    course=[]
    for value in dic.values():
        course+=value
    return course  

def most_courses(dic):
    valuedic={}
    for value in dic.values():
        valuedic[value]=len(value)
    mostcourse=[]
    for key in valuedic.keys():
        if valuedic[key]==max(valuedic.values()):
            mostcourse+=key
        else:
            continue
    desired=''
    for key in dic.keys():
        if dic[key]==mostcourse:
            desired=key

    return desired        

1 Answer

Michael Hulet
Michael Hulet
47,913 Points

Your problem here is on line 26. In this case, dic.values will give you a list of lists, so value in the loop will be a list. This isn't necessarily 100% true, but in general, the keys in a dictionary should always be a string, but in your loop, you're trying to use value (which is a list) as a key, which isn't something you can do