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

Why is this wrong?

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.

I know i can use the len() function but still

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

3 Answers

Steven Parker
Steven Parker
229,645 Points

Before you can add something to a variable, the variable must be created. So before you start the loop, create "count" by assigning it with the value you want it to begin with.

Jordan Hoover
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Jordan Hoover
Python Web Development Techdegree Graduate 59,268 Points

You need to define count outside of the loop first. Also, should definitely use len().

def num_teachers(a):
    count = 0

    for key in a.keys():
        count += 1
    return count
Shreyas Papinwar
Shreyas Papinwar
2,371 Points

Hey you didn't create the loop -

def num_teachers(dictionary):
    count = 0

    for i in len(dictionary.keys()):
        count += 1
    return count
```python
Steven Parker
Steven Parker
229,645 Points

:x: This code is not a valid solution. Try it in the challenge and see!

And Namrata did create a loop, just not the count.

Oh, and thanks for formatting your code, but the language name only goes at the top.