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

In my own editor this returns Jason Seifer, the challenge editor says it returns Kenneth Love

So it's not passing. Any thoughts?

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.
teachers_dict = {
'Jason Seifer': [
'Ruby Foundations', 'Ruby on Rails Forms', 'Technology Foundations'],
 'Kenneth Love': [
 'Python Basics', 'Python Collections']
 }

jason = []
kenneth = []


def most_classes(teachers_dict):
    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

Try moving jason and kenneth into the function.

...

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)

I just tried that, thank you... but it's still not passing. Jusy so II understand your thought process, What were you thinking that would do?

Moving the variables into the function makes them part of the local scope. Since that is the only place that you are using them, it is the only place that you need them defined. Defining them somewhere else could result in problems when your code runs.

This is the code that I used to pass the challenge.

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
  • final_teacher, class_count = teacher, len(classes)
  • I might just be losing brain cells, but I don't recognize this syntax.
  • I do get it about scope though. Thanks

Your code is 10 lines shorter than mine. I'm gonna try'n figure what you did. I don't think we covered .items() but it looks pretty handy. It puts the dict in a list in tuples?

final_teacher, class_count = teacher, len(classes)
# is the same as
final_teacher = teacher
class_count = len(classes)

You nailed what dict.items() does.