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

error--not reading answer to task 2

This should find the correct answer, 4. What is wrong with your system?

my_dict = {'Andrew Chalkley': ['jQuery Basics', 'Node.js Basics'], 'Kenneth Love': ['Python Basics', 'Python Collections']}

def num_courses(dict):
values=list(my_dict.values()) val0 = values.pop(0) val1 = values.pop(0) return (len(val0) + len(val1))

num_courses(my_dict)

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.
my_dict = {'Andrew Chalkley': ['jQuery Basics', 'Node.js Basics'],
       'Kenneth Love': ['Python Basics', 'Python Collections']}

def num_teachers(dict):
        return (len(dict))

num_teachers(my_dict)

def num_courses(dict):   
    values=list(my_dict.values())
    val0 = values.pop(0)
    val1 = values.pop(0)
    return (len(val0) + len(val1))

num_courses(my_dict)   

2 Answers

You're going in a direction I wouldn't recommend. You want to count the length of each course list and add them up. You can access the course lists using the dict.values() method and iterating through the values.

def num_courses(dict_arg):
    counter = 0
    for value in dict_arg.values():
        counter += len(value)
    return counter

Thanks for your reply but the problem here is that you get a value of 2 when the task is seeking an answer of 4--the total of all the courses. I have tried iterating through the values previously and always got hung up with 2 which is why i thought to get from a list of lists to individual lists. The other thing I considered was trying to flatten the lists to one single list, but trying to figure out how to do this. At any rate, my method does return 4, so i don't understand why it was not accepted. There was no constraint that was to be put upon us for method o get the correct answer

I got the correct value & passed with my code. Not sure why yours isn't passing.