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

THIS ONE IS FOR TEAMTREEHOUSE staff: Bug in WorkRoom not letting me continue.

Bug in teachers.py is not letting me continue, seem like workroom changes what the expected output is so if I hit RECHECK WORK several times it eventually works but when more code is added then the "expected output" for previous one is changed again so I'm getting nowhere.

Just to make things easier here to explain I am currently uploading a video showing the exact issue. I'll update this ticket in few mins with the link to the video I'm preparing.

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

def most_classes(teachers):
    max_count = 0
    la_lista = teachers.values()
    TheTeacher = ''

    for key in teachers:
        for numCourses in la_lista:
            if (len(numCourses)) > max_count:
                max_count = len(numCourses)
            else:
                max_count = max_count
            if max_count == len(numCourses):
                TheTeacher = str(key)
    print("The teacher with most clases is: ",TheTeacher)
    return(TheTeacher)

def num_teachers(teachers):
    cuenta = 0
    for key in teachers:
        cuenta = cuenta + 1
    return(cuenta)

Here's the video showing the issue: http://bit.ly/1x0xU4v

Can't proceed from here of course until this is fixed.

7 Answers

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

The problem isn't with Treehouse, it's with your code. You're going through the dictionary twice, once as for key in teachers and once as teachers.values(), which you then go through with another for loop. Remember the problem with order in a dictionary? AKA, they don't have an order? You're likely comparing one teacher's courses while holding onto another teacher's name.

You don't need two for loops for this at all. Either use .items() or just look up the list of courses based on the key you're on.

Kenneth my code might not be perfect but a completely different code is now giving me the same issue and they both run locally on my computer and "sometimes" runs in workroom.

The key here regarding the "sometimes runs in workroom" is that workroom seem to be expecting different result every time so whenever it actually accepts it (it only accepts it when 'Jason Seifer' is the result) then it goes to stage 2 but when is re-run there what happens is that whatever dictionary it is using is expecting a different result. Does this make sense?

I have uploaded another video so that you can see what I see, here it is: http://bit.ly/1IgvZkP

Let me ask it in a different way.... If only teachers on my dictionary are "Kenneth Love and Jason Seifer" where are "Dave McFarland and Pasan P." coming from? I know the are treehouse teachers but they are not in my dictionary so how come workroom some times expect them to be the result of code execution?

Thanks.

and... just to make the point (that I think something is not right on regards to what workroom expect) would you share a code that works for you so that I can do with it same test I showed in my video? As I said, code used now is completely different to the initial one and result is exactly the same on regards to the fact that work room is not working with the dictionary I'm testing (the one in the example) but is bringing names (not in my dictionary) so it's got to be checking agains another dictionary.

Oh well.. as a matter of fact, if you check what dictionary workroom is checking against (that includes names from other treehouse teachers) and share it I will just try my codes with the right dictionaries.

Thank you.

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

One thing that it seems you're not understanding. No matter what you define for teachers outside of your function, when my validation code calls most_classes, it uses its own input argument. So I pass my own dictionary to your function, which is why you're getting names that aren't in your example. Like I said on Twitter, the example values are exactly that, an example. They're not the actual input.

Here's code that passes it for me:

def most_classes(teachers):
    high_count = 0
    high_teacher = None

    for teacher, classes in teachers.items():
        if len(classes) > high_count:
            high_count = len(classes)
            high_teacher = teacher
    return high_teacher

As you see, I only go through the dictionary once. If you didn't want to use .items(), then something like:

for teacher in teachers:
    if len(teachers[teacher]) > high_count:

would work fine.

Kenneth thank you so much for clarification. If you noticed during my testing I was also putting my own dictionary into the code and maybe I shouldn't (is that what you are saying?) so that my code is validated against yours in workroom. If this is what you are saying then: I totally understand now the issue I was facing.

Now..., as for the "for teacher, classes in teachers.items" I know (by some additional reading ) that we could use the "for k, v" approach (same as you suggested it in "for teacher, classes" ) but I think that is covered in your course AFTER the exercise where I've been struggling lately.

I think same might apply (honestly I'm not sure about this one) to the .items() which I researched after your previous response.

Anyway, thank you so much for clarification, I'll be spending some more hours on this today.

PS: your dinner menu sounded delicious, hope you enjoyed it ;)

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Honestly, I'm not certain where .items() appears in the course. I know it's there but I'm not sure what step it was revealed. Anyway, like I showed, perfectly solvable without using .items() or the for k, v in blah loop.

BTW I totally understand now what you said. Basically as part of my code I was using my own dictionary and that was not what workroom was checking against so thank you very much for clarification. I will revise again if I find where the items and K,V are mentioned in case this might be fixing in the order for future students but for now I got what I needed so really appreciate your time and clarification on this.

Actually...., would you believe that EXACT SAME ISSUE is replicable with your code?

Give me few mins and I'll upload a video here for you.

PS: I assume you've been watching my videos all the time :p since that's really the best way I can think of to show you what I see here.

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Ah, yeah, I made a typo. Lemme fix.

To clarify, I did return teacher, which returns the currently-being-inspected teacher. I fixed that mistake and changed it to return high_teacher which will only return the teacher with the highest number of classes.

Here's the video I've just uploaded for you showing you what I meant: "that EXACT SAME ISSUE is replicable with your code"

VIDEO HERE: http://bit.ly/1wa65Xo

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Like I said, my original post had a small, but important typo/mistake. I was returning teacher, which would return the last teacher that the loop went through in the teachers dict, instead of returning high_teacher which would return the teacher with the highest number of courses. I fixed that typo in the post.

Thank you for clarification. Another question, when you said that I was " going through the dictionary" more than once you were referring to more than once within the same function, right? I guess I will still have to go over it again on every other function required as part of this exercise, right?

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

You were doing two for loops through your dictionary. You looped through the keys with for key in teachers and then you made an iterable of the values and looped through that with for numCourses in la_lista. Dictionaries aren't guaranteed to come out in any specific order when they're accessed, so you're likely going to end up having one teacher's name while you're looking at a different teacher's courses. There might be something else going on in there, but that seems to be the most obvious problem/error.