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

Hussein Amr
Hussein Amr
2,461 Points

I don't get the question

any ideas

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):
    inters = []
    for k,v in COURSES:
        if k & v:
            inters.append(k)
    return inters

1 Answer

Ismail KOÇ
Ismail KOÇ
1,748 Points
if k & v:

this statement intersection compares keys and values, this challenge wants intersection your argument (topics) and values of COURSES for write course name (COURSES[key]) and return intersection.

if input_arg & COURSES[key]:

check this and write what do you need.

Michael Hulet
Michael Hulet
47,913 Points

Hi Ismail! For future reference, it's frowned upon around here to write a copy/paste answer with no explanation at all. If you'd like, feel free to re-post your code, but be sure to thoroughly explain why it's correct. Also, in Python, the & does a bitwise AND operation, not a logical and, which is probably what you want. A logical and in Python is just the word and. Happy coding, and thanks for helping out around the Community!

Ismail KOÇ
Ismail KOÇ
1,748 Points

i will pay attention this for next time

Hussein Amr
Hussein Amr
2,461 Points

Ismail KOÇ did that but didn't work

def covers(topics):
    for v in COURSES:
        if topics & (COURSES[v]):
            return topics.intersection(COURSES[v])
Ismail KOÇ
Ismail KOÇ
1,748 Points
def covers(topics):                                  # ------>    No space
    for v in COURSES:                                # ------>    4 spaces 
        if topics & (COURSES[v]):                    # ------>    8 spaces
            return topics.intersection(COURSES[v])   # ------>    12 spaces

python has space sensitive for ex:

for i in range(1,5)           # loop 1  -----> No space 
    for j in [6,7,8]          # loop 2  -----> 4 space
        for k in (9,0)        # loop 3  -----> 8 space

in this program :

  • loop 3 in loop 2
  • loop 2 in loop 1

look your code again:

def covers(topics):                                  # ------>    No space
    for v in COURSES:                                # ------>    4 spaces 
        if topics & (COURSES[v]):                    # ------>    8 spaces
            return topics.intersection(COURSES[v])   # ------>    12 spaces
  • for loop in (covers) function
  • if condution in for loop
  • return in if condution
  • so you need to move return 8 space backward (delete 8 spaces) because return needs to independent of for loop.