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

Set Math Part 2 Question

Having issues passing part 2 of set math code challenge...

I've used the ".union" comparison method but am having the "Didn't get the right output from covers_all" error message thrown. Halp?

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):
    lst = []
    for course in COURSES:
        if topics.intersection(COURSES[course]):
            lst.append(course)
    return lst


def covers_all(this_set):
    lst = []
    for course in COURSES:
        if this_set.union(COURSES[course]):
            lst.append(course)
        return lst

3 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

A union of two sets will always yield a set, therefore the if condition will always be True.

Instead of union, use intersection again, but this time verify that all of the topics are covered by the course. Hint: how would the size of the new intersection compare to the size of the topics set if all topics were found in the course?

Post back if you need more help. Good Luck!!!

Edit: Also, the return statement is indented inside the for loop making the function return after the first iteration.

Chris, you're a machine dude!

I actually ended up using .issubset for the set operator.

Return statement indent was definitely tripping me up, thanks for pointing out that 'dent' in my code ; )

Ismail KOÇ
Ismail KOÇ
1,748 Points
def covers_all(this_set):
    lst = []
    for course in COURSES:
        if this_set.union(COURSES[course]):      
        ''' that if statement makes problem, this if statement has to compare 
        intersection of this_set and COURSES[course])'''
            lst.append(course)
        return lst

that if statement makes problem, this if statement has to compare intersection of this_set and COURSES[course]) for add to lst (this challenge wanted to us : "Return the names of all of the courses, in a list, where all of the topics in the supplied set are covered.") so length of intersection has to equal length of this_key

Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

"Giving" away the correct code is frowned upon in the Community, as it does not foster any type of learning. Code that is not accompanied by a detailed explanation of why it is correct and / or what the Student asking the question did wrong will be deleted by Moderators.

Ismail KOÇ
Ismail KOÇ
1,748 Points

Sorry for about that, i could not be careful, i will be careful next time for this issue.

I was also able to get this to work using .issubset() instead of .union()