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

I got the right answer, but it is wrong..

Challenge Task 1 of 2

Let's write some functions to explore set math a bit more. We're going to be using this COURSES dict in all of the examples. Don't change it, though! So, first, write a function named covers that accepts a single parameter, a set of topics. Have the function return a list of courses from COURSES where the supplied set and the course's value (also a set) overlap. For example, covers({"Python"}) would return ["Python Basics"].

I have tested my code using python 3.5 and it works just fine. However, when I submit my answer I get "Bummer, Didn't get the right answer from covers". I have no idea what this could be failing on, so any help would be much appreciated.

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(arg):
    val = []
    for course in COURSES:
        if arg.issubset(COURSES[course]):
            val.append(course)
    return val

1 Answer

Umesh Ravji
Umesh Ravji
42,386 Points

Hi Brandon, while your answer is correct for the given example, it isn't correct if you test with a different set, say {"Python", "Java"}, as you will get no results back (instead of ['Java Basics', 'Python Basics']).

In this question you shouldn't be looking to see if the arg set is a subset of the courses set, but you should be using intersection to see if there is any overlap at all.

If I remember correctly, checking for a subset is the secound task :)

Thanks for clarifying that for me, I now understand what is being asked here :P