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 (Retired) Dictionaries Teacher Stats

Andrew Gordon
Andrew Gordon
10,178 Points

Challenge 4/4: Courses offered by all teachers.

I keep getting the error "You returned 5 courses, you should have returned 18" and I am not sure why this is so. I understand that the teachers dict can be larger than just what we see presented in the comments at the top, but the loop should hit every 'value' in the dict, and then append the course_name list with said 'value', no matter how many there are in the dict??

Even if I play a little with:

def courses(teachers):
    course_names = []
    for x, y in teachers.items():
       course_names.append(y)
    return course_names

I still receive the same error, not that I was expecting a fix, but I was at least expecting a throw of some different kind.

Thanks for any suggestions ahead of time!

teachers.py
# The dictionary will be something like:
# {'Jason Seifer': ['Ruby Foundations', 'Ruby on Rails Forms', 'Technology Foundations'],
#  'Kenneth Love': ['Python Basics', 'Python Collections']}
#
# Often, it's a good idea to hold onto a max_count variable.
# Update it when you find a teacher with more classes than
# the current count. Better hold onto the teacher name somewhere
# too!
#
# Your code goes below here.

def most_classes(teachers):
  maxNum = 0 
  name = ""
  for x, y in teachers.items():
    if len(y) > maxNum:
      name = x
      maxNum = len(y)
  return name

def num_teachers(teachers):
  count = 0
  for x, y in teachers.items():
    count += 1
  return count

def stats(teachers):
  name = []
  for x, y in teachers.items():
    new_name = []
    new_name.append(x)
    new_name.append(len(y))
    name.append(new_name)
  return name

def courses(teachers):
  course_name = []
  for x in teachers.values():
    course_name.append(x)
  return course_name

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,426 Points

The teacher's course return for x, y in teachers.items(): is a list. Using append is creating a list of lists, so 5 lists for 5 teachers. Try changing to extend():

       course_names.extend(y)
Kyle Cohne
Kyle Cohne
1,880 Points

Thank you. I knew it had to be a simple thing.