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

Dezhi Zhu
Dezhi Zhu
2,662 Points

please help!

This challenge has several steps so take your time, read the instructions carefully, and feel free to experiment in Workspaces or on your own computer. For this first task, create a function named num_teachers that takes a single argument, which will be a dictionary of Treehouse teachers and their courses. The num_teachers function should return an integer for how many teachers are in the dict.

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(*wargs,**kwargs):
    a = []
    for key,value in kwargs.items():
        a = a.append(key)

    return(len(a))

1 Answer

AJ Salmon
AJ Salmon
5,675 Points

Hiya Dezhi!

Firstly, your function only needs to take a single argument, and because it's already a dictionary, there's no need to pack it into one.

def num_teachers(teacher_dict):

Then, I like your idea of adding a list and appending it with the name of the teacher, then returning the length, but that doesn't necessarily work, and there's a much simpler way to do it. You won't need to use .items() but you will need .keys(), and here's a hint: you can use len() on teacher_dict.keys(). Good luck!