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

Mar Bocatcat
Mar Bocatcat
7,405 Points

Stuck on 'Stats Challenge' task 3 of 4

I cant seem to figure this one out. I am new to thinking like a programmer so any type of help is appreciated!

Output: Bummer! Didn't get the expected output. Got ['Andrew Chalkley',4].

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(dictionary):
  max_count = 0
  teacher_name = ''
  for teacher in dictionary:
    len_class = len(dictionary[teacher])
    if (len_class) > (max_count):
      max_count = len_class
      teacher_name = teacher
  return teacher_name

def num_teachers(dictionary):
  for teachers in dictionary:
    num_teachers = len(dictionary)
  return num_teachers

def stats(dictionary):

  new_list = []
  for teachers in dictionary:
    list_teachers = teachers
  for classes in dictionary:
    list_class = len(dictionary[classes])
  new_list = [list_teachers, list_class]
  return new_list
Mar Bocatcat
Mar Bocatcat
7,405 Points

The one i am having trouble with is the last function 'def stats(dictionary'. Please let me know if i missed any details. Thanks!

1 Answer

What is your intent with list_teachers and list_class? It seems like you want to append values, but right now, you're just assigning the most recent value (so that list_teachers gets overwritten X times, rather than having X items in an array).

Mar Bocatcat
Mar Bocatcat
7,405 Points

Thanks for the reply! Sorry i am really new to this kind of thinking, I intended to put the key and the value to their list and then combine them in a (name, classes ) kind of way. Since the objective is to list them that way

Sure! In that case, try the .append() method for list items -- so list_teachers.append(teachers). If you get stuck on a challenge, it's often useful to open up a Workspace to try to accomplish a similar task - it might not be exactly the same as the challenge, but it could give you some useful information!

I looked over the challenge as it's been a while, and I think you'll want to review the "dictionary iteration video." When you call the key for the dictionary, you have access to all the information you need - so you can do a single for loop inside the stats() function.... Remember to initialize new_list, as you've done, then for each key in the dictionary, append a list (remember, using the square brackets []) of the teacher name and number of classes. (You calculated those before for the most_classes() function.)

And there's definitely no need to apologize for being new -- we all start out as beginners!

Mar Bocatcat
Mar Bocatcat
7,405 Points

Thank you so much for your help! Okay i took your advice and I thought i figured it out but still unable to get the output that they want.

def stats(listNames): teachers = []

for teach in listNames:
    teachers.append(teach)
    classes = len(listNames[teach])
    teachers.append(classes)

print teachers

The output is [Name, Number of classes , Name, Number of classes ] but its still wrong. Im not sure what im doing wrong.

Rad! You're really, really close! Look back at the original question: the challenge is asking for a list of lists. Can you make the item you're appending in each iteration (currently, the teacher's name and # of classes) into a list that has that teacher's name and # of classes?

Mar Bocatcat
Mar Bocatcat
7,405 Points

I did this: ef courses(listNames): courses = [] for teacher in listNames: courses.append(teacher) classes = listNames[teacher] courses.append(classes)

print courses

and it worked! Thank you so much for your help! Im glad you didnt give me the answer and just gave me hints. You really helped me a lot! Thanks again!

Great job! One of the things that I find really exciting about programming is that different people can solve the same problem in different ways. Mine looked more like this:

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

I'm glad that you kept working at it until you found a solution!

Mar Bocatcat
Mar Bocatcat
7,405 Points

Yours look a lot cleaner than mine was! Thank you again for the help , i might need your help again if i get stuck again -_-

Thanks!