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

Jacob Hill
Jacob Hill
2,805 Points

Had to check multiple times to pass the task? Task 4

My complete code.

def most_classes(x):
    high = 0
    current = 0
    name = None
    for item in x:

        if len(x[item]) > current:
            high = len(x[item])
            current = high
            name = item
        else:
            current = len(x[item])
    return name
def num_teachers(y):

    return len(y)

def stats(z):
    ourlist = []
    containerlist = []
    for item in z:
        containerlist = [item, len(z[item])]
        ourlist.append(containerlist)

    return ourlist

def courses(zz):
    courselist = []
    for item in zz.values():
        courselist.extend(item)

    return courselist

I've tested all of this through the workspace running each part multiple times and it always gave me the correct value. But for me to get task 4 to pass in the challenge I had to check the task at least 10 times before it accepted it. Is there something wrong with my code? If there is something wrong could you explain to me why it's wrong so I can better understand and not make the mistake in the future?

Also, and aside from the variable names I know those are horrid, is my code itself okay? I'm new to this whole thing and would like to know if I'm progressing well.

Thanks in advance.

1 Answer

Vidhya Sagar
Vidhya Sagar
1,568 Points

There is a small logic lapse in your code for challenge 3. Now when the first for loop iteration is over containerlist ,holds the name and number of subjects for the first person in the key .When the second iteration is happening,the containerlist still has this value ,which should not be happening. It should be empty ,Then only when you use append to it it will only be holding the values of the second person in the dict. So try this ,Hope it was useful.

def stats(z):
    ourlist = []

    for item in z:
        containerlist = []
        containerlist = [item, len(z[item])]
        ourlist.append(containerlist)

    return ourlist
Jacob Hill
Jacob Hill
2,805 Points

Thank you! That's all it took. Amazing how something so simple can cause things like that. A lesson to be learned for sure.