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

need some help.... i already got the problem 1/5 solved.. but when i insert solution to problem 2/5... gets messed up

challenge task already resolvd.. when i insert solution to challenge task 2/5... i get get the "bummer: didn't get the number of teachers right".. the code is already working on my i.d.e...

teachers.py
dict_val = {"eboni williams": "jenkins", "jillian mele": "python dev", "carley shimkus": "ruby dev", "hillary vaughn": "artificial intelligence", "lisa booth": "machine learning", "jedediah bilah": "jenkins", "emelie ikeda": "jenkins"}

def num_teachers(dict):
    num_list = []
    for key in dict_val.keys():
       num_list.append(key)
    return len(num_list)

def num_courses(dict):
    list_0 = list(dict.values())
    my_dict = {}
    for item in list_0:
        if item in my_dict:
            my_dict[item] = 1
        else:
            my_dict[item] = 1
    return sum(my_dict.values())

result = num_teachers(dict_val)
result2 = num_courses(dict_val)

print(result)
print(result2)

1 Answer

Steven Tagawa
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Steven Tagawa
Python Development Techdegree Graduate 14,438 Points

Hi Emil,

So I see a couple of things...

First, your num_teachers function looks like it should work, but you really don't have to go to all the trouble of creating a list and appending each key to it ... all you have to do is return len(dict) which gives you the number of entries in the dictionary.

The other thing is that the dictionary that you're using to test out your functions, dict_val, is NOT in the same format as the sample dictionary the challenge uses. I've copied that from the first task and pasted it below:

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 dict_val has a single string as the "course" value for each teacher; the actual challenge dictionary has a LIST of courses for each teacher. To start off, you might want to copy the example dictionary (it only has two entries, but that's enough) and use it to test your functions.

(Oh, and you might also want to look at that if/else block... if your 'if' and your 'else' do exactly the same thing, then it's always going to do that thing... so there's no reason to have an if block at all...)

Hopefully this gets you closer to passing the challenge. Just keep at it, and you'll end up getting it!