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

Abdulkadir Kollere
Abdulkadir Kollere
1,723 Points

Sets issues

The code below gives me the desired result in workspaces as usual but does not allow me to pass the challenge. Kindly assist

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(elem):
    keys = COURSES.keys()
    for item in elem:
        for key in keys:
            values = COURSES[key]
            if item in values:
                print (key)

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 67,989 Points

You have the correct idea. The function needs to return a list of the results. One way is to initialize an empty list at the top of the function, then append() each found course name instead of printing the matches. Finally, return the result.

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

Abdulkadir Kollere
Abdulkadir Kollere
1,723 Points

I did the changes as suggested, but it still says did not get the right output from 'covers'. See the code below:

def covers(elem):
    keys = COURSES.keys()
    total = []
    for key in keys:
        values = COURSES[key]
        if elem & values:
            total.append(key)
            return(total)
Chris Freeman
Chris Freeman
Treehouse Moderator 67,989 Points

So close! The return statement is indented too far. In this position, the function will return after the first match. To fix it, the return should align with the for statement.

Abdulkadir Kollere
Abdulkadir Kollere
1,723 Points

That worked. I often get in trouble with the indentations. Thanks alot!!