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

Task 1 no longer passes.

In Stage 3 Dictionaries, I pass Task 1 of the challenge but when I go do task 2, task 1 no longer passes. What gives?

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):
    num_classes = 0
    max_classes = 0
    teacher = 0
    for k in dictionary:
        for v in dictionary.values():
            num_classes = len(v)
            if num_classes > max_classes:
                max_classes = num_classes
                teacher = k
    return teacher

def num_teachers(dictionary):
    number_teachers = 0 
    for k in dictionary:
        number_teachers += 1
    print number_teachers

num_teachers(dictionary)

6 Answers

Thomas Kirk
Thomas Kirk
5,246 Points

Hi,

Your code is printing the number of teachers instead of returning it. Also, I think you'll need to take out the:

num_teachers(dictionary)

line at the bottom of your code.

A simpler answer to that challenge might be:

def num_teachers(dictionary):
    return len(dictionary.keys())
Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

Task 1 isn't passing because you introduced an error in Task 2. You used the print statement instead of the print() function.

Ok this explains it. I did finally get it to work. Thanks for your answers; I'm so surprised to get an answer from the instructor! So, question from a fellow Portlander and PyLady: I do my tasks in Notepad++ as I work through them and for some reason, return won't work for me...it only works if I use print, which is how that error got in there. I know I have to change every print statement to return when I switch to Treehouse, and I know that functions should always return something, so why doesn't it work in Notepad++/Python 2.7/windows powershell?

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

How does return not work? Does it throw an error?

It doesn't throw an error but nothing happens. It just gives me a new prompt and doesn't return anything from the program. When I switch all the returns to print statements, it works and then when I switch to Treehouse, I need to change them all back to returns. Sometimes I forget and it takes me a few tries.

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

So you're just not getting any feedback? You shouldn't see return values unless you print them or assign them to a variable and then print that variable.

Try this:

def test():
    return "Hi!"

print(test())

Oh, that works! I wonder why I've never encountered that before? I thought that to call a function, you just would type:

test()

Well, thank you. This is helpful. And thanks for doing this class. The other PDX PyLadies say you are pretty awesome. I hope to meet in person someday if I can stick with this.

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

return sends a value back out of a function into whatever called it. Maybe it's a variable, maybe it's another function. If it's nothing, the value just goes away and you'll never see it. That's why you weren't seeing the values before. Just call your functions inside of a print and you should see the values.

We're having a meetup at the Treehouse office on 1/21 if you want to attend. It's over Vim.

I think I'd like to go to that! Where can I get the details?