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 trialNamrata Lamba
1,347 PointsWhy 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
# 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
231,248 PointsBefore 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
Python Web Development Techdegree Graduate 59,268 PointsYou 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
2,371 PointsHey 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
231,248 PointsThis 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.