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

Thananjaya Chakravarthy
Thananjaya Chakravarthy
4,672 Points

Create a new function named num_courses that will return the total number of course

I have tried in many ways... but cannot able to get the right thing

Anyone, please help me!!

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(teachers):
    count=[]
    for keys in teachers.keys():
        count.append(keys)
    total=len(count)
    return total


def num_courses(teachers):
    total_courses = []
    end_result = []
    for key in teachers.items():
        total_courses.append(teachers.values())
    for courses_of_one_teacher in teachers.values():
        for course in courses_of_one_teacher:
            end_result.append(course)
    return end_result    

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,426 Points

You are SO close. Task 2 is asking for the count of courses so it's looking for a number not a list of courses. The quick fix is to wrap the return value in len():

    return len(end_result)

The better answer would be to rework the code to focus on counting the courses instead of compiling a list of them. Hint: len(some_dict) returns the number of items in a dict