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

Haitao Huang
Haitao Huang
2,834 Points

Number of teachers challenge. Don't understand why my code doesn't work.

This challenge has several steps so take your time, read the instructions carefully, and feel free to experiment in Workspaces or on your own computer.  For this first task, 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.

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(argument):
    argument = {'Andrew Chalkley': ['jQuery Basics', 'Node.js Basics'],
                'Kenneth Love': ['Python Basics', 'Python Collections']}
    return int(len(argument)) 

4 Answers

David Chacón
David Chacón
1,086 Points

When you return len(x) it should give you an integer anyway. Also the workspace is going to supply the argument when you click "check work".

Think more about a variable that you can add integers to and then return. I supplied my code, but... spoilers if you want to figure it out on your own.

SPOILERS

def num_teachers(teachers):
    count = 0
    for teacher in teachers:
        count += 1
    return count
Haitao Huang
Haitao Huang
2,834 Points

Thank you, David! It is extremely helpful to know how the workspace work.

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

Roberts worked for me.

teachers = {'Andrew Chalkley': ['jQuery Basics', 'Node.js Basics'], 'Kenneth Love': ['Python Basics', 'Python Collections'], 'Bruno Marcelo': ['Python Basics', 'Python Collections'], 'Maria da Penha': ['Varios nada', 'Vagabundagem'] }

def num_teachers(teachers): total = 0 for k, v in teachers.items(): if k in k: total = total +1 return total print(total)

num_teachers(teachers)