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

Jen Farrant
Jen Farrant
6,905 Points

takes 0 positional arguments but 1 was given

This code passes in Thonny (when I use 'print' instead of 'return'), but fails in the code challenge with 'TypeError num_teachers takes 0 positional arguments but 1 was given.

Despite Googling all the key terms I do not understand what this error code is telling, and I don't understand why it works outside of the code challenge.

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.
teachers = {'Andrew Chalkley': ['jQuery Basics', 'Node.js Basics'],
'Kenneth Love': ['Python Basics', 'Python Collections'],'Jane Smith':['Flask basics', 'Django Basics']}

def num_teachers():
    total_teachers = list(teachers.keys())

    return(len(total_teachers))

num_teachers()

2 Answers

Stuart Wright
Stuart Wright
41,120 Points

You received this error because the function you defined does not take any arguments, but the challenge expects it to take one argument (the dictionary of teachers). You simply need to include teachers as an argument to your function, like this:

def num_teachers(teachers):
    total_teachers = list(teachers.keys())
    return(len(total_teachers))
Jen Farrant
Jen Farrant
6,905 Points

Thanks Stuart - I thought I had tried that, but I was obviously doing it wrong.

that is now working !