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 (2016, retired 2019) Dictionaries Teacher Stats

Can someone help me out with this challenge?

Wow, I just can't stump you! OK, two more to go. I think this one's my favorite, though.

Create a function named most_courses that takes our good ol' teacher dictionary.

most_courses should return the name of the teacher with the most courses. You might need to hold onto some sort of max count variable.

arg = {'Andrew Chalkley': ['jQuery Basics', 'Node.js Basics'],
'Kenneth Love': ['Python Basics', 'Python Collections']}

def most_courses(arg):
    counter = 0
    for arg.values() in arg.keys():
        if len(arg.values()) >= counter:
            counter = len(arg.values())
        else:
            continue
    if len(arg.values()) in arg.keys() == counter:
        return arg.keys()

I don't know why this doesn't work.

1 Answer

Daniel Medina
Daniel Medina
13,863 Points

You have the right idea, but there are a few issues with your code.

The first issue is that you are attaching () in places where they aren't methods. If you type in your function as is into a shell, you'll get

File "<pyshell>", line 3
SyntaxError: can't assign to function call

This is due to your use of arg.values on the third line. What you should have is something more like:

for key, value in arg.items():
    if len(value) > counter:
        counter = len(value)

This allows you to loop through a dictionary using key value pairs. Note that we can simply check if a value is > another rather than using >= in this situation.

You don't really need your else block.

Also, remember that the challenge is asking for the teacher. You'll need to have a string variable for your teacher outside of your for loop. You can re-assign it later based on whatever your counter / maximum count is.

Keep it up, you're almost there!