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

This is driving me crazy!! Teachers challenge...

Already tried different ways, and sometimes accepts my code, but then in task 2 says that task 1 is no longer passing, because the answer is always changing. I think it's because dicts change, but i can't really understand what's missing or wrong!

def most_classes1(teachers):
  list=[]
  high_classes = 0
  high_teacher = ''
  for teacher in teachers:
    for lessons in teachers.values():
        lenght = len(lessons)
        if lenght)>high_classes:
            high_classes=lenght
            high_teacher = teacher
        else:
            high_classes = high_classes
            high_teacher=high_teacher
  return high_teacher
def most_classes(teachers):
  total_stats = []
  teach_stats = []
  list_class = []
  classes0 = 0
  for teacher in teachers:
    teach_stats.append(teacher)
    list_class.append(len(teachers.values()))
    for classes in list_class:
      if classes>classes0:
        classes0  = classes
        index = list_class.index(classes)
        teacher_high = teach_stats[index]
      else:
        continue
  return teacher_high

Any help is welcome, since i'm stuck :S

1 Answer

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

You're on the right track. Let's break it down step by step:

  • Set a high count of 0, because we don't have any other counts
  • Set a high teacher name as blank because we don't have a high teacher yet
  • Loop through all of the teachers in the dictionary. We can do this with for teacher in teachers: and teacher will be a teacher's name
  • Get the length of that teacher's classes. We can do this with len(teachers[teacher]) since teachers is our dictionary and teacher is a key of that dictionary
  • If the length is higher than the high count, change the high count to the length and set the high teacher to teacher
  • When the loop finishes, return the high teacher

Your first example has the first three steps right but gets a little messy after that (and )= isn't valid at all). See if the list above can help you fix the last little bit.

Finally! Thanks a lot! Ended up like this:

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

The other one had some stupid things from all of my attempts, like de else part and the typo and your help was precious :D