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

Julian Silvestri
Julian Silvestri
4,214 Points

Teacher Stats Challenge.... im so confused

I am trying to do the first challenge of the 5 required... cant seem to pass it... Not sure what I am missing...

please help

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(arg1):
    number_of_keys = 0
    for x in arg1.keys():
        number_of_keys += 1
    return number_keys # <- [MOD: should be number_of_keys - srh]

3 Answers

Hi Julian,

If you access the dictionary using its .keys() method, you can pass that into a len() method and return the result. No need for a loop.

Something like:

def num_teachers(dict):
    return len(dict.keys())

I hope that makes sense.

Steve.

Incidentally, your code is fine but you returned a variable of a different name. I'll add a comment in the code in your question.

This is the same as your code and works fine.

def num_teachers(dict):
    count = 0
    for key in dict.keys():
        count += 1
    return count

No problem! This set of challenges is tough!! :+1:

Herman Brummer
Herman Brummer
6,414 Points
def num_teachers(dictionary_list):
    # number of teachers
    return len(dictionary_list)