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

Joshua Dam
Joshua Dam
7,148 Points

Not getting the right number of teachers

I found someone else's question and noticed they were using "return len(teachers_courses.keys())"

I used that instead and it worked. But why does that work vs. the method I tried using? I get the same number returned in Workspaces.

teachers.py
teach_dict = {'Andrew Chalkley': ['jQuery Basics', 'Node.js Basics'], 'Kenneth Love': ['Python Basics', 'Python Collections']}

def num_teachers(teachers_courses):
    number_teachers = 0
    for teachers in teach_dict.keys():
        number_teachers += 1
    return number_teachers

1 Answer

Okay, since you're actually defining your own teach_dict I'm going to assume you were testing this on your own system and passing that dictionary to your function for testing purposes. Following that logic, when you toss teach_dict to num_teach() as teacher_courses, you never actually do anything with it. It (teachers_courses) just sits there waiting while you reference the original variable that was defined outside of the function teach_dict, which is why the challenge isn't completing for you. If your goal is to complete the challenge, you need to remove your self-defined dictionary at the top, then actually use teachers_courses in your for-loop, then it should pass just fine.

Joshua Dam
Joshua Dam
7,148 Points

Oh man, I didn't even realize I did that. Late night coding. Thank you!