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

Anthony Lopez
Anthony Lopez
963 Points

Returning the total number of courses for all teachers question!

Hey guys!

I'm trying to figure out why my function def num_courses(sing_arg) isn't working. When I type it out in my desktop application I get the correct number of classes, but the challenge is not accepting my answer in team tree house. I know it can be very particular.

Am I missing something? Thanks!

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(sing_arg):
    return len(sing_arg)

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

The second loop isn't necessary because you can get the length of the courses with len.

    for courses in sing_arg.values():
        value += len(courses)
    return value
Cooper Runstein
Cooper Runstein
11,850 Points

Your answer passed when I copied and pasted it in, I'm not sure why it isn't passing.

2 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

The originally posted code does not pass due to an error:

line 16
    if answer in courses:
                        ^
TabError: inconsistent use of tabs and spaces in indentation
def num_teachers(sing_arg):
    return len(sing_arg)

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

Maybe I missed a typo or something. This code passes.