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

Austin Cumberlander
Austin Cumberlander
13,997 Points

What's wrong with my code in the courses function?

The code I've written for the courses function works in my text editor. I don't understand what's wrong.

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.keys())

def num_courses(courses):
    return sum(len(course) for course in courses.values())

def courses(courses):
    courses_list = []
    for course in courses.values():
        courses_list += courses

    return courses_list

1 Answer

Stuart Wright
Stuart Wright
41,118 Points

You have two errors in this, and they are both on the same line:

courses_list += courses
  • You can't use += to add items to a list, you need to use a list method to do this.
  • You have named your loop variable course, and this is what needs to be added to the list, not courses.

I'll leave you to see if you can work out the rest, but let me know if you need any more help.

Austin Cumberlander
Austin Cumberlander
13,997 Points

Nevermind. I just refreshed the page and it worked. Thanks anyway!

Stuart Wright
Stuart Wright
41,118 Points

I just realised that the first of my points above wasn't valid - you can indeed use += to add to list (although it's not the only way). The second point was definitely accurate though. Anyway glad you got it to work!