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

Kyle Hahnel
seal-mask
.a{fill-rule:evenodd;}techdegree
Kyle Hahnel
Python Web Development Techdegree Student 5,670 Points

Error: Returned 5, expected to return 18. Teacher Stats Challenge question.

I am getting a strange error message when attempting to run the code for the 4th part of the Teacher Stats Challenge. As far as I can tell, the code is working. It is returning the correct number of courses available in the dictionary, which is 5. Any help is appreciated!

dict = {'Jason Seifer': ['Ruby Foundations', 'Ruby on Rails Forms', 'Technology Foundations'], 'Kenneth Love':
        ['Python Basics', 'Python Collections']}

def most_classes(dict):
  count = 0
  teacher = ""
  for items in dict:
    classes = len(dict[items])
    if classes > count:
      count = classes
      teacher = items
  return teacher   

def num_teachers(dict):
  return len(dict)  

def stats(dict):
  lists = []
  try:
    for item in dict:
      new_count = 0
      for second_item in dict[item]:
        new_count += 1
      lists.append([item,new_count])
  except:
    You = True
  return lists  

def courses(dict):
  all_courses = []
  for course in dict:
    all_courses.append(dict[course])
  return all_courses

3 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,428 Points

For the courses() function, the dictionary passed in contains teachers as keys and their courses as a list. Your code is actually building a list of lists. So the top level element count will be equal to the number of teachers in the dict.

The code below uses extend() to add the elements of the teacher's course list to your all_courses list.

def courses(dict):
    all_courses = []
    for teacher in dict:  #<--  change loop variable to "teachers" for clarity
        all_courses.extend(dict[teacher])  #<-- changed append to extend
    return all_courses
Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

You're appending the list of courses to your returned list. That means the returned list's length will be however many sublists are in it. Look at this:

returned_list = [[1, 2], [3, 4, 5], [6, 7, 8, 9]]

The length of returned_list is 3. You need to .extend() your returned list with the list for each teacher instead of .append()ing it.