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) Sets Set Math

Bruce Röttgers
Bruce Röttgers
18,211 Points

My code seems to be working, is it my fault?

Hey there,

this seems to be working locally and I tested it with 3 different sets, do you see the problem

sets.py
COURSES = {
    "Python Basics": {"Python", "functions", "variables",
                      "booleans", "integers", "floats",
                      "arrays", "strings", "exceptions",
                      "conditions", "input", "loops"},
    "Java Basics": {"Java", "strings", "variables",
                    "input", "exceptions", "integers",
                    "booleans", "loops"},
    "PHP Basics": {"PHP", "variables", "conditions",
                   "integers", "floats", "strings",
                   "booleans", "HTML"},
    "Ruby Basics": {"Ruby", "strings", "floats",
                    "integers", "conditions",
                    "functions", "input"}
}

def covers(topics):
        courses = []
        for course in COURSES.keys():
            val = COURSES[course]
            if (topics & val) != set():
                        courses.append(course)
        return courses

2 Answers

Hi Bruce, I'm not sure what you are trying to say but, I tested your code and it works just fine in code challenge...

This is how i solved it, and it's similar to your solution:

def covers(topics):
    list_of_courses = []

    for course_title, course_topics in COURSES.items():
        if topics & course_topics:
            list_of_courses.append(course_title)

    return list_of_courses

Just a side note here: in Python you probably should indent code blocks with four spaces. this is found in the official Style Guide for Python Code, also known as "PEP 8". This document lays out style conventions that most professional Python programmers follow. Its goal is to promote readability and consistency in Python code bases. Also there is a course here at "TreeHouse" explaining this, you can find it here: Write Better Python

In some IDEs ( "Integrated Developer Environments" ) this will be the case .. if your IDE is not configured to use "4" spaces for indentation you can modify it in your IDE settings. For me, I'm using "PyCharm" and the default configuration is using "4" spaces == ( "1" tap ).

It's better to indent a code with spaces.. see this answer: Why do you indent a code with spaces instead of tabs?

In Workspaces you can modify indentation level in the bottom right corner where you can find the word "Spaces", it's default value is set to "2". Just click on "2" and set it to "4".

For example the code you wrote here:

def covers(topics):
        # "8" spaces
        courses = []

        # "8" spaces
        for course in COURSES.keys():
            # "4" spaces
            val = COURSES[course]
            # "4" spaces
            if (topics & val) != set():
                        # "12" spaces 
                        courses.append(course)

        # "8" spaces
        return courses

and this is my code:

def covers(topics):
    # "4" spaces
    list_of_courses = []

    # "4" spaces
    for course_title, course_topics in COURSES.items():
        # "4" spaces
        if topics & course_topics:
            # "4" spaces
            list_of_courses.append(course_title)

    # "4" spaces
    return list_of_courses