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

Daniel Smith
Daniel Smith
10,172 Points

why is my covers_all function incorrect?

am i not supposed to use issubset? the direction is kinda getting less and less so i found that on stack overflow, its supposed to return the keys that cover all of a given sets topics

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(topic):
    topic=set(topic)
    output=[]
    for key in COURSES.keys():
        if topic & COURSES[key]:
            output.append(key)
    return output

def covers_all(*topic):
    topic=set(topic)
    output=[]
    for key in COURSES.keys():
        if topic.issubset(COURSES[key]):
            output.append(key)
    return output
Daniel Smith
Daniel Smith
10,172 Points
def covers_all(topic):
    topic=set(topic)
    output=[]
    for key in COURSES.keys():
        if not topic-COURSES[key]:
            output.append(key)
    return output

is the code i tried again and it worked in pycharm but not in the website.. it even works if i changed the arguments

1 Answer

Louise St. Germain
Louise St. Germain
19,424 Points

Hi Daniel,

The only issue with the second version of covers_all that you posted in the comment is that there is a tab instead of spaces in front of the line with output=[]. Since everything else is indented with spaces, this looks different to Python and causes problems, especially in the Treehouse challenges.

The first version of covers_all wasn't working for two reasons. One is that same tab problem I just mentioned above, and the other is that you create a tuple instead of a set when you do

def covers_all(*topic):

with the asterisk before topic. Since we know from the challenge that they will pass a set to the function, you don't need to modify the format in any way, so you can get rid of that asterisk and just use the set as is.

(You can actually get rid of the topic=set(topic) line also, since it's already going to be a set and that operation is redundant - but it doesn't hurt to leave it.)

Changing those two (or one) little things should get it working! Good luck! :-)