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

Jesse Miller
Jesse Miller
7,920 Points

Not sure where I'm going wrong for courses(dicts) (Last part of challenge).

Did I go wron gin my if else statement?

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(dicts):
  most_class = ""     # holds the name of teach with most class
  max_count = 0       # max counter for classes
  for teacher in dicts:
    if len(dicts[teacher]) > max_count:
      max_count = len(dicts[teacher])
      most_class = teacher
  return most_class

def num_teachers(dicts):
  num = 0
  for i in dicts:
    num+=1
  return num

def stats(dicts):
  list_of_lists = []
  for i in dicts:
    for q in dicts[i]:
      list_of_lists.append([i, len(dicts[i])])
  return list_of_lists

def stats(dicts):
  list_of_lists = []
  for i in dicts:
    for q in dicts[i]:
      list_of_lists.append([i, len(dicts[i])])
  return list_of_lists

def courses(dicts):
  single = []
  for i in dicts:
    for q in dicts[i]:
    if q not in single:
      single.append(dicts[i][q])
    else:
      continue
  return single

2 Answers

Aby Abraham
PLUS
Aby Abraham
Courses Plus Student 12,531 Points
def most_classes(dict):
    max_count = 0
    str = ""
    alist = dict.values()
    for key, value in dict.items():
        if len(value) > max_count:
            max_count = len(value)
            str = key
    return str


def num_teachers(dict):
    return len(dict.keys()) 


def stats(dict):
    slist = []
    for key, value in dict.items():
        slist.append([key, len(value)])
    return slist


def courses(dict):
    clist = []
    for value in dict.values():
        clist.extend(value)
    return clist
Steven Parker
Steven Parker
231,269 Points

Here's a few hints:

  • you have defined the stats function two times (oddly, that doesn't actually affect the challenge checker)
  • it looks like you have an indentation error starting at: if q not in single:
  • your append is trying to use strings as numeric indices (did you mean to write: single.append(q) ?)

When you fix these, I think you'll have it.

I'd also add that, while adding the if statement is a great idea and will work, it's not actually needed to pass this challenge.

Also, this won't effect the challenge but single letter variables like i and q aren't very "Pythonistic". You should aim to use descriptive variables! like

def course(teachers):
    course_list = []
    for teacher in teachers:
        for course in teacher:
            #etc etc  

Good luck!

Tayler

Steven Parker
Steven Parker
231,269 Points

About that if statement: the challenge does not use the same data as in the example to test your function, so we don't know for sure if having it would make a difference before trying it (but as Tayler says, it doesn't).

However, the challenge was not specific about making the list unique names or not. If the object were to create a list of unique names, the if statement would be important. On the other hand, if the object were to compile a cumulative list, the if statement would need to go. We just don't know, but I think your choice of a unique list is the more practical one.