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

pet
pet
10,908 Points

In challenge Task 2:5 I'm getting "Bummer: Didn't get the right number of courses!"

Here's my code:

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(tec_dict):
    tecs = 0 
    for item in tec_dict.keys():
        tecs += 1
    return tecs

def num_courses(tec_dict):
    courses = 0
    for value in tec_dict.values():
        courses += 1
    return courses    

3 Answers

Every teacher has a certain number of courses. Not all of them have one course. So why are you adding 1 to courses for every value? :wink:

Try adding len(value) to courses every iteration instead.

I hope this helps! :grin:

Happy coding! :zap: ~Alex

jorgelpezguzmn
jorgelpezguzmn
6,830 Points

I am not sure if you want to get the overall number of teachers and courses. But in that case, here is the code:

def num_teachers(tec_dict):
    return len(tec_dict.keys())

def num_courses(tec_dict):
    courses = 0
    for value in tec_dict.values():
        courses += len(value)
    return courses  

I hope this helps too!

nakalkucing
nakalkucing
12,964 Points

Hey jorgelpezguzmn, I know you just wanted to help. But just so you know, Treeehouse asks that we do not simply give away the code. They request that when answering a question you use hints like Alex did above. :) Happy coding, Nakal

jorgelpezguzmn
jorgelpezguzmn
6,830 Points

I'm sorry. I did not know that. Thanks for the advice!

pet
pet
10,908 Points

Thanks for all the help.