Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Python Collections (2016, retired 2019) Sets Set Math

Create a new function named covers_all that takes a single set as an argument. Return the names of all of the courses, i

Need help on what I am doing wrong.

Thanks,

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(python):
    list = []
    for course, value in COURSES.items():
        if value.intersection(python):
            list.append(course)
    return list 

def covers_all(1set):
    list = []
    for course, value in COURSES.items():
        if len(set(1set) & (COURSES[keys])): 
            list.append(course)
             return list

2 Answers

Robert Stefanic
Robert Stefanic
35,157 Points

Without checking the logic too much, I see a few issues here that need to be fixed.

  1. A variable in Python cannot start with a number. So the argument name that you've defined in covers_all needs to be changed to a valid variable name (you can just change 1set to set1).

  2. On this line:

    if len(set(1set) & (COURSES[keys])): 
    

    There's an extra ) before your colon.

  3. Your return statement is inside of your if statement. So as soon as something matches this condition if len(set(1set) & (COURSES[keys])):, it's going to append the current course that was checked, and return the list with just one element.

  4. Where did [keys] come from in (COURSES[keys])? This may be a Python thing that I'm unaware of, but did you mean for it to be values which was defined in your for loop?

Go ahead and make those minor changes, and see if you can't figure it out from there. :)

Eduardo Valencia
Eduardo Valencia
12,444 Points

When you used the "&" symbol in the following line, you are checking if both of them evaluate to True, and then checking the length of the boolean value.

if len(set(1set) & (COURSES[keys])):

This is not what you need. Think about the challenge instructions. It is asking you the following:

  1. To look into the given set of topics, and to intersect that with each course value in COURSES (like you did in your "covers" function).
  2. To check that all items in the given set of topics are in the course value. The intersection function gives you back the items it found in common between the two sets. You have both the results and the items you wanted. What do you think you need to do to know if the items you wanted (the given set of topics) is equal to the results? If that is true, then what do you think you need to do next with the name of the course?