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

Alejandro Byrne
Alejandro Byrne
2,562 Points

Why doesn't my function work?

Hi, sorry, another question. I tried to get the course by seeing if the topics were in union instead of just if topics in course:. Help?

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(topics):
    result = []
    for topic in topics:
        for course_key, course in COURSES.items():
            if topic in course:
                result.append(course_key)
    return result

def covers_all(topics):
    result = []
    for course_key, course in COURSES.items():
        if topics in topics.union(course):
            result.append(course_key)
    return result

1 Answer

Claudio Longato Jr
Claudio Longato Jr
4,318 Points

I couldn't understand if you wanted to check if you're trying to check if the topic is in all courses. For example, you check if the topics 'integers', or 'Python' is in all courses.

I'm sending a working function that answers: Which of the topics (in the argument [list]) are present in all courses?

def covers_all(topics):

result = []

for topic in topics:
    # counter
    is_in_course = 0

    for course_key, course in courses.items():
        if topic in course:
            is_in_course += 1

    if is_in_course == len(courses):
        result.append(topic)

return result