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

BOHAN ZHANG
BOHAN ZHANG
4,446 Points

Python Stuck

That one wasn't too bad, right? Let's try something a bit more challenging. Create a new function named num_courses that will receive the same dictionary as its only argument. The function should return the total number of courses for all of the teachers.No clue why my function does not work foe the second question, the out put is correct on my local https://teamtreehouse.com/library/python-collections-2/dictionaries/teacher-stats

code

teachers = {'Andrew Chalkley': ['jQuery Basics', 'Node.js Basics'],  'Kenneth Love': ['Python Basics', 'Python Collections']}
def num_teachers(teachers):
    count = 0
    for d in teachers.keys():
        count = count+1
    return count


num_teachers(teachers) 

def num_courses(teachers):
    count = 0
    a = teachers.values()
    my_list2, my_list1 = zip(*a)
    list_total = my_list2 + my_list1
    for d in list_total:
        count = count + 1
    return counnt

num_courses(teachers)

1 Answer

Steven Parker
Steven Parker
231,269 Points

:warning: Be careful about testing a challenge in an external REPL.
The actual test data will almost certainly not be the same as the sample shown in the instructions.

This line in particular struck me as extremely "brittle":

    my_list2, my_list1 = zip(*a)

What if the actual argument has different numbers of classes for each teacher? And what if there are more than 2 teachers? Both of these are quite likely to be the case in the real test data.

Also, for the challenge you only need to define the functions, you don't need to call them (or define any global variables).