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

Jamaru Rodgers
Jamaru Rodgers
3,046 Points

Returning list of course names

Can't get the program to extract the values of the dict into a single list. Used dict.values(), but maybe in the wrong place. Please HELP!

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(arg):
    count = len(arg)
    return count
def num_courses(arg):
    total = 0
    for teacher in arg.values():
        for course in teacher:
            total += 1
    return total    
def courses(arg):
    course_list = []
    for teacher in arg.values():
        for course in teacher:
            if course == True:
                course_list.append(course)
            else:
                continue
        break
    return course_list

1 Answer

Dan Garrison
Dan Garrison
22,457 Points

Your code is a bit too complicated. You don't need the if/else statements or the break. It looks like you are trying to check if the teacher has courses with your code, but it shouldn't append an empty list if the teacher doesn't have any courses.

Jamaru Rodgers
Jamaru Rodgers
3,046 Points

I know that if/else conditional is complicating. I actually started without it in the code, but then I got an error saying that I wasn't catching all the courses so I added it in to make sure I did. But It's still throwing that error.

Dan Garrison
Dan Garrison
22,457 Points

Your code should work if you take if/else and the break out of your code, but here is what I used to pass the challenge.

def courses(arg):
    course_list = []
    for teacher in arg.values():
        for course in teacher:
            course_list.append(course)
    return course_list

If you tried that already then it is likely some indenting or other syntax error. It might help if you test your code out in a code editor and command line.

Jamaru Rodgers
Jamaru Rodgers
3,046 Points

I swear that's the code I tried before I did the crazy if/then statement, but this time around it seemed to work lol. Thank you for you help man!