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

Quinton Dobbs
Quinton Dobbs
5,149 Points

I'm confused on part 3 of Python Collections(Teacher Stats)

The task is:

Great work! OK, you're doing great so I'll keep increasing the difficulty. For this step, make another new function named courses that will, again, take the dictionary of teachers and courses. courses, though, should return a single list of all of the available courses in the dictionary. No teachers, just course names!

I'm getting the error "Couldn't find courses'", but the only thing it explicitly says to name "courses" is the function, which I have done, and I haven't referenced "courses" anywhere in my code so I'm a little confused. Thanks.

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(teachers):
    return len(teachers)

def num_courses(teachers):
    count = 0
    for value in teachers.values():
        count += len(value)
    return count

def courses(teachers):
    course_list = []
    for course in classes.values():
        for course in value:
            course_list.append(course)
    return course_list

1 Answer

andren
andren
28,558 Points

The error message is a bit misleading, the challenge checker will often claim that it can't find a function if the function contains some error that makes it unable to run the code within the function in question.

In your case the issue is that you have misnamed two of your variables. For your first loop you refer to the dictionary as classes when it's actually called teachers within the function. And you also call the variable for the loop course instead of value which it seems like you meant to type based on the rest of the code.

If you fix those two issues like this:

def courses(teachers):
    course_list = []
    for value in teachers.values():
        for course in value:
            course_list.append(course)
    return course_list

Then your code will work.

Also just as a tip you can actually simplify this function a bit as the second for loop is actually unnecessary. You can append the list of courses directly to the course_list variable similarly to the way the code in the num_courses function works.

Like this:

def courses(teachers):
    course_list = []
    for value in teachers.values():
        course_list += value
    return course_list
Quinton Dobbs
Quinton Dobbs
5,149 Points

Awesome! Appreciate the help. I must have gotten myself confused going around trying to change things to get it to work.