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

Devendra Singh
Devendra Singh
3,337 Points

My code is working in workspace but getting error in challenge task

if you pass a set of topics then function covers return list of courses which have overlapping topics with passed parameter. it working fine but still getting error

Q: 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"].

Ans:

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(param1): myList=[] for key, value in COURSES.items(): if (param1 & value) == param1: myList.append(key) return myList

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(param1):
    myList=[]
    for key,value in COURSES.items():
        if (param1 & value) == param1:
            myList.append(key)
    return myList 

1 Answer

Hello

I have to admit that this challenge has the worse error message I've seen so far. Even if the code actually works (e.g.; meet the definition of the example provided in the question texy), you will still fail the challenge.

try this code

def covers(param1):
    myList=[]
    for key,value in COURSES.items():
        for item in param1:
            if item in param1.intersection(value):
                myList.append(key)
    return myList

def covers_all(param1):
    myList=[]
    for key,value in COURSES.items():
        if param1.intersection(value) == param1:
            myList.append(key)
    return myList

print(covers({"Python", "Java"}))
print(covers_all({"conditions", "input"}))

you don't need the print statements but I included them so you can test and review. Hope this helps.

Devendra Singh
Devendra Singh
3,337 Points

Thanks.. It's working..