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

Dillon Reyna
Dillon Reyna
9,531 Points

Why do I keep getting a 'couldn't find num_teachers' error?

.

teachers.py
def num_teachers(diccio):
    for key in diccio.keys():
        teachers += 1
    return teachers

1 Answer

Hi there,

I believe there may be a syntax error occurring inside the function, which is why it's not picking it up - I see the counting variable there, but it's never initialized - teachers += 1 is the same as teachers = teachers + 1, so it's trying to add something that doesn't have a value yet. If you add one line, you can take this approach and it'll work:

def num_teachers(diccio):
    teachers = 0  #add this line
    for key in diccio.keys():
        teachers += 1
    return teachers

It's also worth noting that you can return the length of the dict in a single line using len(), if you're interested:

def num_teachers(diccio):
    return len(diccio)