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

to me this is clearly wrong, but it passed the clallenge

I couldn't get it to pass so I switched the final if clause. Now it returns the wrong thing in my own editor, but it passed the challenge. Something strange is afoot with the challenges.

teachers.py
teachers_dict = {
'Jason Seifer': [
'Ruby Foundations', 'Ruby on Rails Forms', 'Technology Foundations'],
 'Kenneth Love': [
 'Python Basics', 'Python Collections']
 }




def most_classes(teachers_dict):
    jason = []
    kenneth = []
    for key in teachers_dict:
        if key == "Jason Seifer":
            for course in teachers_dict[key]:
                jason.append(course)
        else:
            for course in teachers_dict[key]:
                kenneth.append(course)


    if len(jason) < len(kenneth):
        return "Jason Seifer"
    else:
        return "Kenneth Love"


result = most_classes(teachers_dict)
print(result)

1 Answer

I'm not sure why the code works that way, but this is the way that I solved that challenge. It's a bit more reusable and shorter.

def most_classes(teachers):
    final_teacher = None
    class_count = 0
    for teacher, classes in teachers.items():
        if len(classes) > class_count:
            final_teacher, class_count = teacher, len(classes)
    return final_teacher

I like this code. It has some snippets that I have not seen covered yet. I'm going through it line by line.

john larson, if you have any questions, I'd be happy to try and explain the code.

I looked up .items(), that looks like a handy method.

  • final_teacher, class_count = teacher, len(classes)
  • final_teacher, (does this comma mean and?)
  • so would that read...
  • final_teacher and class_count = teacher and len(classes)

I think that's a pretty good way of putting it.

so then you are comparing final_teacher with teacher and class_count with len(classes) ?

That's exactly what I'm doing.

New but related question, Jac... I've gone through the code and I'm wondering this...before you started did you know you would have final_teacher set to none? And that you for/if would be structured the way they are? Or was it a process that you came to?

Before you started did you know you would have final_teacher set to none?

I set final_teacher to None right away. It's a good default to use, and I feel that it would be better than an empty string.

Before you started did you know that your for/if would be structured the way they are or was it a process that you came to?

I feel like the for loop and if block were more of a process that I arrived at. The original code that I would have had would have done the same thing and wouldn't have looked too different, but I don't think that this was my original code. I'm pretty sure that I did originally have the teachers.items() in the for loop.