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

My code works (tried it many times with different combos) but the Recheck work function doesn't like it ... can sm help?

Hi,

Please see below my code, which works absolutely perfectly when I run it in the terminal with any set_of_topics combo. Weirdly the Recheck work function doesn't seem to like it... it might be a bug that's why I have asked my question here rather than on slack although I migh also I missed smth here so I am sorry if that's the case.. Thanks in advance.

def covers(set_of_topics):

    relevant_courses = []

    for course in COURSES :
        if set_of_topics.issubset(COURSES[course]):
            relevant_courses += [course]

    return relevant_courses
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(set_of_topics):

    relevant_courses = []

    for course in COURSES :
        if set_of_topics.issubset(COURSES[course]):
            relevant_courses += [course]

    return relevant_courses

5 Answers

Grigorij Schleifer
Grigorij Schleifer
10,365 Points

Hey Raphael, I replaced the issubset() method by the intersection() method and it worked fine. Sometimes the interpreter is picky about the code. Even though it works fine somewhere else.

But I think that the issubset() method returns a boolean if every element in x is in y. The intersection method returns a new set with the items that intersect. For the next challenge you will need a set of items and not a logical.

Let me know if this helped.

Thank you so much for your help and for the explanations about the key differences between .issubset() and .intesection() methods. That's really helpful !

I found a code that is accepted by the workspace in another thread which was a bit different that my code but produces the same output! Thanks a lot guys. Still not 100% sure why my code is not accepted though..

Sure - Here you go mate :

def covers_all(set_of_topics):

    relevant_courses = []

    for course in COURSES :
        if set_of_topics.issubset(COURSES[course]):
            relevant_courses += [course]

    return relevant_courses

Also worth adding that I used my code for covers_all (2nd sub challenge) and it worked !

Grigorij Schleifer
Grigorij Schleifer
10,365 Points

Very interesting, can you share the code please? I would like to see the logic used. Thank you Raphael

Oh thanks so much Grigorij ! It's very good to know wich methods are the most appropriate for a specific problem and indeed what you say makes a lot of sense to me. Merci !