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

Keifer Joedicker
Keifer Joedicker
5,869 Points

Not sure why this code wont pass task 2

It works in outside environments but for some reason it wont pass through the task?

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(a):
    return len(a.keys())

def num_courses(a):
    courses = {}
    for i in a:
        if type(a[i]) == type("String"):
            courses[i] = 1 
        else:
            courses[i] = len(a[i])

    print(courses)

2 Answers

james south
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
james south
Front End Web Development Techdegree Graduate 33,271 Points

well you are printing instead of returning, but also you don't need to type check, and you don't need to create a dictionary, you are just counting courses. create a counter variable and add to it 1 for each class in the values of the dictionary.

Keifer Joedicker
Keifer Joedicker
5,869 Points

okay I wasn't quite sure what they were asking for so I went all out. Thank you

Grant Tribble
Grant Tribble
8,210 Points

Hi Keifer, I feel like you're going a little too far on this one. First, for your function num_teachers: all you need is to return(len(a)) there's no need for all of that key() stuff. Second, for your function num_courses: if you were to make your var courses an int and assign it a 0... you could just write a for loop (for each in a:) that would continue to add ( courses += ) the len(a[each]). Then, you would just have to return(courses) when you finished your for loop.

Keifer Joedicker
Keifer Joedicker
5,869 Points

I only did a.keys() because I thought typing len(a) would reference the keys and the values as opposed to just the keys