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

Abdullah Jassim
Abdullah Jassim
4,551 Points

I ran this on the shell and it returned the right answer. Not sure why my code is wrong.

def num_courses(arg):
    count_courses = 0
    for keys in arg.keys():
        count_courses += 1
        return count_courses
teachers.py
def num_teachers(arg):
    teachers = int(len(arg))
    return teachers

def num_courses(arg):
    count_courses = 0
    for keys in arg.keys():
        count_courses += 1
        return count_courses

# 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.

1 Answer

Philip Schultz
Philip Schultz
11,437 Points

Here is how I did the num_courses challenge. I looped through the dict and counted the length of the values.

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