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

Sandy Leon
Sandy Leon
2,246 Points

How to iterate through set parameter?

Hello everyone, I am up to the first challenge with set math and I have gotten as far returning the proper course, BUT only if the set parameter is one value long. when I try to add another it just returns None. I have been stuck for a good minute, any help is greatly appreciated.

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(set):
    for course, topic in COURSES.items():
        for all(x) in set:
            if set.issubset(topic):
                return course

1 Answer

Jeff Muday
MOD
Jeff Muday
Treehouse Moderator 28,720 Points

It looks like you have a grasp of the concepts of what is to be done. A couple of small tweaks to your code:

  • create an empty list of courses you want to return to the user
  • the inner loop is just "x in set" a little simpler than what you had--

this allows us to go through each item in the set.

  • check if x is in the topics for the current course

if it is, append the current course to our courses list.

  • finally, return the courses we collected.
def covers(set):
    courses = [] # list of courses to return
    # the next block goes through all the courses and topics.
    for course, topics in COURSES.items():
        # the next block looks to see if each of the x in the set are in the topics
        for x in set:
            # on the next line, check if x is included in the topics
            if x in topics:
                courses.append(course) # add the course to our courses list
    return courses # finally, return the courses to the calling function.
Sandy Leon
Sandy Leon
2,246 Points

Ahh dang it! I keep misusing/ misplacing 'return' and as a result get confused with my function's output. Thank you, Jeff, for your time and thorough answer.