Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Python Collections (2016, retired 2019) Dictionaries Teacher Stats

I'm getting a Type Error on Challenge Task 1 of 5

This is the error that I am getting: TypeError: num_teachers() takes 0 positional arguments but 1 was given

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(**numberofTeachers):
    for teachers in numberofTeachers.key():
        return numberofTeachers.count(key)

2 Answers

Easy one liners to solve it will be these reply if you need more explanation

num_teachers = lambda arg: len(arg.keys())
num_courses = lambda arg: sum([len(c) for c in arg.values()])
courses = lambda arg: [c for s in arg.values() for c in s]
most_courses = lambda arg: [t for t, c in sorted(arg.items(), key=lambda a: len(a[1]), reverse=True)][0]
stats = lambda arg: [[t, len(c)] for t, c in arg.items()]
jonlunsford
jonlunsford
12,405 Points

Your function is expecting keyword arguments such as:

num_teachers(name='Ken', classes=['Python', 'Collections'])

not positional arguments such as:

num_teachers(dict)

I can't tell from your code, but my guess is you are trying to pass in a dictionary which is a positional argument since it is not unpacked by Python. Here's how I would handle the assignment:

def num_courses(teachers):
for key in teachers:
courses = len(teachers[key])
print("{} teaches {} courses.".format(key, courses))

then call the function using the teachers dictionary:

data = {'Andrew Chalkley': ['jQuery Basics', 'Node.js Basics'], 'Kenneth Love': ['Python Basics', 'Python Collections']}
num_courses(data)

the output would look like the following:

Kenneth Love teaches 2 courses.
Andrew Chalkley teaches 2 courses.

This is assuming you are counting classes for each teacher. From this you should be able to revise your code for counting the number of teachers.