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

BRIAN WEBER
BRIAN WEBER
21,570 Points

Write a function named courses that takes the dictionary of teachers.

Currently getting "Bummer! You returned 5 courses, should have returned 18."

Could someone please explain why this is?

Thanks!

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_dict):
  max_count = 0
  busiest_teacher = ''
  for teacher, classes in teachers_dict.items():
    if len(classes) > max_count:
      max_count = len(classes)
      busiest_teacher = teacher
  return busiest_teacher

def num_teachers(teachers_dict):
  teacher_count = 0
  for teacher in teachers_dict:
    teacher_count += 1
  return teacher_count

def stats(teachers_dict):
  newList = []
  for teacher in teachers_dict:
    newList.append([teacher,len(teachers_dict[teacher])])
  return newList

def courses(teachers_dict):
  courseList = []
  for classes in teachers_dict.values():
    courseList.extend([classes])
  return courseList

2 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

Your courses function needs some help:

def courses(teachers_dict):
    courseList = []
    for classes in teachers_dict.values():  # gets a **list** contain teachers courses.
        # extends courseList the contents of your [] list. which is a single item: the teachers course list
        courseList.extend([classes])  # <-- fix by removing "[" and "]" from around `classes`
    return courseList

Without the change your adding lists to courseList. 5 teachers, 5 lists. With the change your code extends courseList with the contents of the list classes: Now all 18 courses are at the same level in courseList

Aside: course_list would be a more Pythonic variable name. Camel case isn't a python thing except for class names.

BRIAN WEBER
BRIAN WEBER
21,570 Points

Chris, thank you very much!