Python Python Collections Dictionaries Teacher Stats

num_teachers challenge: why doesn't this code work?

def num_teachers(teachers_courses): teachers_courses = {} number_of_teachers = len(teachers_courses.keys()) return int(number_of_teachers)

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(teachers_courses):
    teachers_courses = {}
    number_of_teachers = len(teachers_courses.keys())
    return int(number_of_teachers)

1 Answer

Henrik Christensen
Henrik Christensen
34,591 Points

All you have to do is to return the number of teachers, and because there's only 1 teacher in each item you can do it very simple like this

def num_teachers(teachers_courses):
    return len(teachers_courses)

# this fails because
def num_teachers(teachers_courses):
    teachers_courses = {}  # here you set teachers_courses to an empty dict
    number_of_teachers = len(teachers_courses.keys())  # getting the length of the empty dict
    return int(number_of_teachers)  # returning 0 because the dict is empty