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

sowmya c
sowmya c
6,966 Points

set math challange 2

it should give the course name of all the values match getting error

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_set):
    courses_list = []
    for course in COURSES.keys():
        if topics_set & COURSES[course]:
            courses_list.append(course)
        else:
            continue
    return courses_list

def covers_all(topics_set):
    all_courses = []
        for values in COURSES.values():
            if topics_set & COURSES[value]:
                all_courses.append(course)
            else:
                continue
    return all_courses

1 Answer

Jeff Muday
MOD
Jeff Muday
Treehouse Moderator 28,722 Points

You might want to use "COURSES.items()" as your loop iterator. It makes it simpler to handle each course and its topics. It's not the only way to do it, but you will see it often enough as a coding pattern.

def covers(topic):
    # notice, I am using the singular "topic" name to clarify we are looking for a single match.
    course_list = []
    # note, I am using COURSES.items() iterator, it makes things a little easier
    for course, topics in COURSES.items():
        if topic & topics:
            # our "topic"  intersects with the course "topics"
            course_list.append(course)
    return course_list

def covers_all(topics_set):
    # notice, we are using topics_set to connote there might be multiple topics we are looking for.
    course_list = []
    for course, topics in COURSES.items():
        if len(topics_set - topics) == 0:
            # the length will be zero if "topics_set" is an exact subset of "topics"
            course_list.append(course)
    return course_list