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

Dylan Fried
Dylan Fried
3,123 Points

Task 2 for teachers.py. My code gets the output of 4 which is the correct numbers of courses. But I'm getting an error

Whats the difference between count = sum(len(v) for v in Teachers.values()) i found on stackoverflow and my code? Both produces 4 but stackoverflow format passes task 2, and mine doesn't.

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.

Teachers = {"Andrew Chalkley": ["jQuery Basics", "Node.js Basics"], "Kenneth Love": ["Python Basics", "Python Collections"]}
def num_teachers(Teachers):
    Teach = Teachers.keys()
    numOfTeachers = len(Teach)
    return(numOfTeachers)


def num_courses(Teachers):
    value = Teachers.values()
    for num in value:
        numOfCourses = len(num + num)
    return(numOfCourses)

1 Answer

Bapi Roy
Bapi Roy
14,237 Points

Your problem is in line

numOfCourses = len(num + num)

You are concatenating two num lists and then getting the length of it. You are also overwriting the value of numOfCourse with each iteration.

You can fix this by changing above line to code to

numOfCourses += len(num)

Don't forge to initiate the numOfCourses variable before for loop.

TRY TO FOLLOW PEP8 code styling :)