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

Why won't this work? Am I using parameters in my function correctly?

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

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"}
}

kwargs = { "Python Basics": {"Python", "functions", "variables",
                            "booleans", "integers", "floatss",
                            "arrays", "strings", "exceptions",
                            "conditions", "input", "loops" }}
def covers(COURSES):
    list = []
    for key, value in COURSES.items():
        if value.intersection(COURSES):
            list.append(key)
    return list

3 Answers

Josh Keenan
Josh Keenan
20,315 Points

You can't call a list list, as it is a reserved word. This will cause a lot of problems for you, here's my solution to the first step:

def covers(topics):
    courses = []
    for keys, values in COURSES.items():
        if topics.intersection(values):

            courses.append(keys)
    return courses

Ok what I'm really trying to understand is the parameter that you used "topics". Why use that. Does topics hold any value? Thanks for reply

Josh Keenan
Josh Keenan
20,315 Points

It can be any word you want, topics in this instance is an argument. This means that it just represents something you need to pass in and is not a variable elsewhere in the program. When the treehouse interpreter runs the code, it passes in courses to the function and courses becomes the actual parameter that is used

Kyle Chivers
Kyle Chivers
9,702 Points

I think what's causing the confusion is that you're thinking about it in a way where you're passing in the 'COURSES' variable as an argument. For this problem, the argument should not be the 'COURSES' variable but rather a set that's passed into the function.

For example, the function call could be:

covers({"functions", "loops"})

And then the output would be:

['Python Basics', 'Java Basics', 'Ruby Basics']

The 'COURSES' variable should be used in the covers() function but it should not be passed in as an argument. It looks like you took the right approach in iterating over the 'COURSES' variable using the items() method but the conditional statement using the intersection() method needs some re-thinking. It's definitely close though!