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

Alexis Douheret
Alexis Douheret
852 Points

Challenge Task - My function is not found

Hi,

I'm at the last challenge task in the "Python collection" course of Kenneth Love, in the dictionary part. I have to create a function named "stats" that takes a dictionary ad single argument and returns a list of list containing each key and value.

My problem is following: An error saying "Bummer! Couldn't find 'stats'" is raised by the interface.

I tried to reindent the code of this function but it did not work out. I really don't see what could be the problem here... I think it's a silly mistake but I just don't see it!

Thank you for your help in advance!

Cheers,

Doht

PS: The course is really interesting and helps a lot for developers of intermediate level in Python

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 most_courses(teachers_courses):
    lng=0
    tc="no teacher"
    for teacher in teachers_courses:
        if len(teachers_courses[teacher])>lng:
            lng=len(teachers_courses[teacher])
            tc=teacher
    return tc

def courses(teachers_courses):
    lst=[]
    for value in teachers_courses.values():
        lst.extend(value)
    return lst

def num_teachers(teachers_courses):
    num=0
    for key in teachers_courses:
        num+=1
    return num

def num_courses(teachers_courses):
    num=0
    for value in teachers_courses.values():
        num+=len(value)
    return num

def stats(teachers_courses):
    lst=[]
    for itm in teachers_courses.items():
        lst.extend([teacher,len(teachers_courses[teacher])])
    return lst

6 Answers

Hi

Think the probe is here:

def stats(teachers_courses):
    lst=[]
    for itm in teachers_courses.items():
        lst.extend([teacher,len(teachers_courses[teacher])])
    return lst



you probably mean  
for teacher in teachers_courses.items():

otherwise, how is the code to know what teacher,len..... is?
Alexis Douheret
Alexis Douheret
852 Points

This is a different problem!

Use .append() method instead of .extend()

Because you want to have a list of lists

Vedran Linić
PLUS
Vedran Linić
Courses Plus Student 18,802 Points

So i have try this code and it still don't work

def stats(teachers_courses):
    maks = []
    for teacher in teachers_courses.items():
        maks.append([teacher, len(teachers_courses[teacher])])
    return maks

i get error TypeError: unhashable type: 'list'

Alexis Douheret
Alexis Douheret
852 Points

Hello Mark,

Yes I saw that mistake after and once I corrected it, it worked and the challenge got completed But still, it's weird that the error raised said that my function could not be found ! My guess is because it didn't recognise "teacher", the code did not consider stats to be a function.

glad you got it resolved.

Vedran Linić
PLUS
Vedran Linić
Courses Plus Student 18,802 Points

I have try this code

def stats(teachers_courses):
    lst=[]
    for teacher in teachers_courses.items():
        lst.extend([teacher, len(teachers_courses[teacher])])
    return lst

but i still get error TypeError: unhashable type: 'list'