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

Mahmoud Othman
Mahmoud Othman
2,602 Points

help with def coureses

Hello can any tell em what's wrong with my courses function ?

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

def num_courses(teachers):
    total = 0
    for value in teachers.values():
        # for the number of courses within that value
        for course in value:
            total += 1
    return total

def courses(teachers, courses):
    list_courses = []
    for value in courses.values:
        for course in value:
            list_courses.extend(course)
    return list_courses

2 Answers

Philip Schultz
Philip Schultz
11,437 Points

Hey Mahmoud, I see a few things. Your function should only have one parameter, because it's receiving only one dict argument. Also, in the for loop, you want to access the values of the parameter, remember when you use methods , they have to have parentheses. Right now you are saying some_dict.value, but you want to say some_dict.values(). Take a look at the code below and let me know if you have any questions.

def courses(teachers):
    list_courses = []
    for value in teachers.values():
        for course in value:
            list_courses.append(course)
    return list_courses
Philip Schultz
Philip Schultz
11,437 Points

Hey, here is another way to do it. This way you only have to use one for loop.

def courses(arg):
    list_of_courses = []
    for value in arg.values():
        list_of_courses.extend(value)
    return list_of_courses

I noticed how you were trying to use extend, which I forgot about. So, I looked up how to use it again and it makes the code more efficient and easier to read. At least in this case.

Julia Beckwith
Julia Beckwith
2,526 Points

You're very close! Since they are all just taking a single dict as an argument, you can change the argument for courses to just one argument. After that, you will only need one "for" statement. Also, don't forget the parentheses at the end of courses.values()!