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 K
Andrew K
13,774 Points

teachers.py 3/3 Can't figure out why I'm getting either "Bummer! Where's stats()" or "Task One is no longer passing"

Hello! I've been stumped on this for a bit and was hoping for a bit of help. Here's my code, but everything was going smoothly until the last function:

def most_classes(teachers):
  high_classes = 0
  high_teacher = ''
  for teacher in teachers:
    if len(teachers[teacher]) > high_classes:
      high_classes += 1
      high_teacher = teacher
    return high_teacher

def num_teachers(teach_dict):
  teach_count = 0
  for name in teach_dict:
    teach_count += 1
  return teach_count

def stats(teachers):
  total_stats = []
  for teacher in teachers:
    teach_stats = []
    teach_stats.append(teacher)
    teach_stats.append(len(teacher[classes]))
    total_stats.append(teach_stats)
  return total_stats

Any help is greatly appreciated!

3 Answers

Hi Andrew,

Your first function is not passing for me. You have high_classes += 1 and it should be high_classes = len(teachers[teacher])

You don't want to increment that variable. You want to set it equal to the number of courses for that teacher.

For your stats function you are referencing a variable classes that doesn't exist. teach_stats.append(len(teacher[classes]))

You want to append the number of courses for the current teacher you're on. teach_stats.append(len(teachers[teacher]))

def stats(teachers):
  total_stats = []
  for teacher in teachers:
    teach_stats = []
    teach_stats.append(teacher)
    teach_stats.append(len(teachers[teacher]))
    total_stats.append(teach_stats)
  return total_stats

You could also write that function like this and get rid of the intermediate teach_stats variable:

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

Also, it's possible to get the length of a dictionary similar to how you do for a list. So really you could simplify the num_teachers function as well. Rather than iterate over each key in the dictionary and increment a counter you could get the length of the dictionary instead.

def num_teachers(teach_dict):
  return len(teach_dict)
Andrew K
Andrew K
13,774 Points

Jason,

Thank you so much for your response. It cleared up a lot, but it also showed me that some things I was doing are redundant! I rewrote it after reading your post, and it all passed. One odd thing, and I don't know if others experience this, but sometimes when my code returns "Task One no longer passed" (as in this instance, after redoing the code) if I just resubmit once or twice... it passes. Weird.

Thanks again, Jason!

Radha Bhambwani
Radha Bhambwani
10,182 Points

Andrew I was also getting that, "Task one is no longer passed" as well, not sure why either.

As to this post, the activity taught me a lot also however I keep getting a syntax error problem in my code:

def stats(teachers):

  teacher_list = []

  for teacher in teachers:

    teacher_list.append([teacher, len(teachers(teacher))

  return teacher_list  

Specifically it points to the return, which I'm not sure about (This is for part 3 btw)

Andrew K
Andrew K
13,774 Points

Hi Radha, Your function looks fine, but I think you are just losing track of your brackets and parentheses. Your append line should look like this:

teacher_list.append([teacher, len(teachers[teacher])])

So you're missing one square bracket (to close off your list) and when you access the key (teacher), that should be in square brackets, not parentheses.

Hope that helps!