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

Ashley Keeling
Ashley Keeling
11,476 Points

can someone help me with sets and this function please

I am not sure why it isn't working and I don't know if I am missing anything from it 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(st):
    sets=[]
    for key in COURSES.items():
        if key in st & COURSES
        sets.append(value)
    return sets

2 Answers

  • You missed a colon in the if statement:
for key in COURSES.items():
    if key in st & COURSES:
        sets.append(value)
  • The value variable is not defined anywhere. I'm guessing value is referring to the values in COURSES. If so, try:
for key, value in COURSES.items():
    # do stuff
  • COURSES is a dictionary, not a set. That's why & operator is not working here. Try:
for key, value in COURSES.items():
    if value & st:
        sets.append(key)

Since you are already iterating through the keys and the items in COURSES, you only need to check if the value and the input st intersect.

  • Lastly, the sets variable in your code is actually a list type. This can be very confusing when someone else is reading your code.
Ashley Keeling
Ashley Keeling
11,476 Points

thanks I did the challenge

hi there, I am unsure if the way I did this challenge was the best way, however, here are some pointers that may help.

  1. firstly you want to create a list to return
  2. then you want to iterate through courses and get the keys and values - the value in Courses is a set
  3. test your passed in set for an intersection with the vaiue from Courses i.e. use &
  4. if that test passes i.e. you have found an intersection then append the key of that key, value pair into the list you created
  5. return the list
def covers(topics):
    course_list = []  
    for key, value in COURSES.items():
    # test for an intersection
   # if True the add the key to the list

A clue for the next challenge is to use an intersection test again but also check the length of that intersection against the length of the passed in set

Hope that helps...