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

Luis Bonilla II
Luis Bonilla II
11,481 Points

teachers.py 4 of 5

Code I have does print if there is more course from one teacher. It is suppose to print both if they are the same? That's the part I'm having trouble with

teachers.py
# The dictionary will look something like:
# {'Andrew Chalkley': ['jQuery Basics', 'Node.js Basics'],
#  'Kenneth Love': ['Python Basics', 'Python Collections']}
#
# Each key will be a Teacher and the value will be a list of courses.
#
# Your code goes below here.
def num_teachers(dict1):
    return len(dict1)

def num_courses(dict1):
    total = 0
    for value in dict1.values():
        for items in value:
            total += 1
    return total

def courses(dict1):
    available_courses = []
    for value in dict1.values():
        for items in value:
            available_courses.append(items)
    return available_courses

def most_courses(dict1):
    for key, value in dict1.items():
        for values in dict1.items():
            if len(value) > len(values):
                return key

2 Answers

Alex Koumparos
seal-mask
.a{fill-rule:evenodd;}techdegree
Alex Koumparos
Python Development Techdegree Student 36,887 Points

Hi Luis,

The code you pasted in your question would fail.

def most_courses(dict1):
    for key, value in dict1.items():
        for values in dict1.items():
            if len(value) > len(values):
                return key

In your outer loop, you iterate through the dictionary, getting the key, e.g., 'Andrew Chalkley' and the corresponding value, e.g., ['jQuery Basics', 'Node.js Basics'].

During each iteration, you create an inner loop where you get a tuple containing the key and value, which you call values for every element in the dictionary again, e.g., the first time you go through the inner loop, the value of values is ('Andrew Chalkley', ['jQuery Basics', 'Node.js Basics']).

You then check if the length of value (the list of courses) is longer than the length of the tuple (which will always be 2: the first element in the tuple will always be the dictionary's key and the second element in the tuple will always be the dictionary's value).

If the outer loop ever gets a teacher with more than two courses, that length will be longer than 2 and the function will immediately return that value.

So, imagine if in the example, Andrew had three courses and Kenneth had four. And let's suppose that the interpreter read Andrew out of the dictionary before Kenneth. It would return three, since three is the first value greater than 2, but the correct answer would be four.

Cheers

Alex

Alex Koumparos
seal-mask
.a{fill-rule:evenodd;}techdegree
Alex Koumparos
Python Development Techdegree Student 36,887 Points

Hi Luis,

If I understand your question, I think you want to know what should the behaviour be if there is more than one teacher with the maximum number of courses. Like in the example dictionary, where the largest number of courses by a teacher is 2, and both teachers have that number of courses.

If so, I think returning any of the teachers with that number of courses would work. Since dictionaries are unordered, the challenge checker can't assume that any particular teacher will be the first to be checked so there is no way to predict whether (for example) Andrew or Kenneth will be logged in your function as the teacher with the most courses.

That's what I tested, and it worked.

Hope that helps

Cheers

Alex

Luis Bonilla II
Luis Bonilla II
11,481 Points

I added code that would show at least one if they are the same and I'm still getting a fail