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

Kushagra Patel
Kushagra Patel
7,740 Points

Help needed

Hi fellow learners I have created this program for the challenge it works fine on my local machine but does not work online. I only ran the function online whole code I pasted for reference only.

teachers.py
def num_teachers(**teacher_dict):
    a = len(teacher_dict)
    return a
teachers_info_Dictionary={'Andrew Chalkley': ['jQuery Basics', 'Node.js Basics'],'Kenneth Love': ['Python Basics', 'Python Collections'],'Kenneth': ['Basics', 'Collections']}
a=num_teachers(**teachers_info_Dictionary)
print("Number of Teachers {}".format(a))

2 Answers

Jeff Muday
MOD
Jeff Muday
Treehouse Moderator 28,720 Points

In a very simple solution, you only need to change out the **teacher_dict to teacher_dict

def num_teachers(teacher_dict): #change this
    a = len(teacher_dict)
    return a
Kushagra Patel
Kushagra Patel
7,740 Points

thanks for your help sir.

Jeff Muday
MOD
Jeff Muday
Treehouse Moderator 28,720 Points

I like that you are thinking about keyword arguments this way, it shows a pretty advanced grasp of an important concept of parameter passing. Sometimes it's a little confusing that '**' operator takes a dict and extracts its contents and passes them as parameters to a function.

The case you demonstrate has syntactic validity (and works as long as you pass the dictionary in referenced as a keyword argument). See below:

a=num_teachers(**teachers_info_Dictionary)

However, the automated grader has a much stricter set of tests which sends input which will make it fail tests.

This is a little oversimplified-- but the way the automated grader works is similar to a Python "assertion" method. You will learn more about this later in Python Testing.

Suppose we make a basic assertion about the "correct_solution" (In reality there are many possible solutions) versus your solution from above.

def correct_solution(test_case):
    return len(test_case)

assert( correct_solution(case_x) == num_teachers(case_x) )

would throw an error, and be flagged as incorrect.

assert(num_teachers(case) == correct_solution(case))
TypeError: num_teachers() takes 0 positional arguments but 1 was given

I hope this helps. Good luck with Python!

Kushagra Patel
Kushagra Patel
7,740 Points

Thanks sir for clearing my doubt but I lost your explanation after the line : your solution from above. what should I do to correct it ? is I am missing anything