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

So, first, write a function named covers that accepts a single parameter, a set of topics. Have the function return a li

can some one show me where is my mistake

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(str):
    for a in COURSES:
        if str in a:
            return a

1 Answer

Jeff Muday
MOD
Jeff Muday
Treehouse Moderator 28,716 Points

This challenge is a little more complex than you would expect. I can see that you do understand tasks 1 and 2. Let's build on that!

The challenge is testing to see if you can do the following tasks:

1. define a function "covers" and send in a set parameter (you got this one)

2. iterate over a set and examine each set member item (both the "course name" and the "course topics")

3. check each item in the COURSES set and see if the intersection of the set you sent in intersects with the course topics. If it does, then add the "course name" to a new set.

SPOILER ALERT BELOW

=============================

def covers(topics):
    cover_set = set() # this will be our return set/list of courses that match
    for a, a_topics in COURSES.items():
        # notice we use COURSES.items() as this will return a (course name) and a_topics (course topics)
        if a_topics.intersection(topics):
            # if the topics intersect, then add a to our set/list of courses that match
            cover_set.add(a)
    # finally, return the covers_set (turn it in to a list, optional)
    return list(cover_set)