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

Y. Kravets
Y. Kravets
10,350 Points

Small Bummer :-)

Hi, so clearly dictionaries are interfering with my sleep today :-) I went on doing this task and got a funny mistake displayed:

"Didn't get the expected output. Got ['Kenneth Love, 2']."

Which would be fine, except I though that that is exactly what the task asked me to produce (teacher's name and the number of classes he has).

It is possible however that I simply misunderstood what is it I was required to do. If anyone has a clarification I'd appreciate it a lot :-)

Cheers guys!

teachers.py
def most_classes(teachers_dict):
  max_count = 0
  final = []
  for teachers in teachers_dict:
    if len(teachers_dict[teachers]) > max_count:
      max_count = len(teachers_dict[teachers])
      final = teachers
  return final

def num_teachers(tea_dic):
  count = 0
  for teachers in tea_dic:
    count += 1
  return count

def stats(teach_dic):
  new_list = []
  stri = []
  for teacher in teach_dic:
    stri = teacher + "," + " " + str(len(teach_dic[teacher]))
    stri = "".join(stri)
    new_list.append(stri)
    return new_list
# 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.

1 Answer

Blaise Gratton
Blaise Gratton
30,213 Points

I think there are two little issues here - I think your return new_list needs to be de-indented two spaces; your function is currently returning new_list every time that loop iterates, so you're only getting one return value.

The second is that you're returning a string exactly identical to an array (which they want). The task says to 'return a list of lists in the format [name, number of classes].' So, by the end of your loop, your function should return:

new_list = [['Jason Seifer', 3], ['Kenneth Love', 2]]

Right now you're building a string to replicate indeces 0 and 1 of this array. Hopefully this helps!

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Very well covered. Couldn't have explained it better myself!