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

Zachary Kaufman
Zachary Kaufman
1,463 Points

I'm stuck...

Hi guys, I am really stuck with this Code Challenge, if anyone can help I'd really appreciate it.

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):
    classes = 0
    top_teacher = "none"
    dictionary = {'Jason Seifer': ['Ruby Foundations', 'Ruby on Rails Forms', 'Technology Foundations'],
                  'Kenneth Love': ['Python Basics', 'Python Collections']}
    for key in dictionary:
        if len(dictionary.value()) > classes:
            classes = len(dictionary.value())
            teacher = dictionary[key]
    return teacher

1 Answer

Seth Kroger
Seth Kroger
56,413 Points

If you have an argument named dictionary being passed in, you don't have to assign dictionary to something else. That just overwrites the data being passed in.

Zachary Kaufman
Zachary Kaufman
1,463 Points

I'm sorry I don't know what you mean, I can't deleted "dictionary" as an argument for most_classes, but if I delete the dictionary dict from lines 14-15 then it still gives me the same error I had before which is, "Bummer! 'dict' object has no attribute 'value'"

Seth Kroger
Seth Kroger
56,413 Points

Sorry, just saw the obvious error an stopped looking further. I think what you're looking for is the length of the value corresponding to the key so in should be len(dictionary[key]) and given that the the teacher is the key you should have the teacher = key, not dictionary[key] which is the list of courses.

Zachary Kaufman
Zachary Kaufman
1,463 Points

For which part? I tried that for the if statement, the classes variable, and the teacher variable and it all gave me errors.

Seth Kroger
Seth Kroger
56,413 Points
def most_classes(dictionary):
    classes = 0
    top_teacher = "none"
    for key in dictionary:
        if len(dictionary[key]) > classes:
            classes = len(dictionary[key])
            top_teacher = key
    return top_teacher
Zachary Kaufman
Zachary Kaufman
1,463 Points

Ah okay I see my problems, thanks for your help!