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

Having trouble

I have the first 3 steps down but I can't seem to figure out the last one. Getting "You returned 5 courses, you should have returned 18." Here is my code: EDIT: It is also saying that task 1 is no longer passing when I try to run the code.

def most_classes(teachers):
         counts = []
         max_classes = 0 
         for key in teachers:
            counts.append(len(teachers[key]))

        for value in counts:
            if value > max_classes:
             max_classes = value 

        for key in teachers:
            if max_classes == len(teachers[key]):
              return key

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:
    total_stats.append( [teacher, len(teachers[teacher])] )
  return total_stats

def courses(teachers):
  return teachers

3 Answers

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

Colin Gaul The Shopping List stage of Python Basics and the list and dictionary parts of Python Collections should be helpful. I'm sure you can get this already, though. Let me start you off.

def courses(teachers_dict):
    output = []
    # loop over all of the .values() in teachers_dict and add their contents into output
    return output

I get the looping part, it's just adding the values to output is getting me.

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Colin Gaul we went over different ways to add lists together or put the content of one list into another list in both Python Basics and Python Collections. I'll suggest watching the first two videos here

When I try testing my code, it's asking "where's courses()?"

I think you would use .extend() right? To add on more things to the list?

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Colin Gaul yes, you'll want to use .extend(). As for the "Where's courses()?", can you show us your code so we can help debug that?

Sure:

def courses(teachers_dict):
  output = []
  for value in teach_dict.values():
    output.extend(teach_dict.values())
  return output
Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Colin Gaul well, looking at your script above, your function takes teachers_dict but you're using teach_dict in the function. If you make those two match up, it should work better.

Also, you want to extend with values, not teach_dict.values(), since you want the current set of values.

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

courses() should return a list of all of the courses, not the teachers, as your function is doing. Each key in the dict is a teacher and the value for a teacher is a list of their courses. So combine all of the values into one list and send that back.

So it would be like courses(**teach_dict), then return the values of them?

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Colin Gaul well, not exactly. We don't want the dictionary's keys as named arguments. We want to look through all of the keys in the dictionary and combine their values into one big, flat list. And then return that list. This is a good challenge to use the dict.values() method.

So something like: for value in teach_dict.values() then return(value + key)?

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

That would definitely be part of the solution, yes.

Still not understanding, is there a video where I can watch again to help?

Got it Kenneth! Thanks for all your help!

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Awesome! Glad you stuck it out and got through