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

Douglas North
Douglas North
10,884 Points

sets.py

It may be that I have exhausted myself today and can't see the obvious. could you direct me towards fixing my code?

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

def covers_all(single_set):
    new_list = []
    for key, value in COURSES.items():
        if single_set & value:
            new_list.append(key)
    return new_list

2 Answers

Jeff Muday
MOD
Jeff Muday
Treehouse Moderator 28,720 Points

I am not sure what you are trying to accomplish with your code, but I noticed two issues:

  1. use of bitwise operators "|" and "&" rather than logical operators such as "or", "and", or "in".

  2. malformed COURSES dictionary. All dictionaries are formed by key-value pairs. {"key" : value}. Typically the key is a string, and the value can be any kind of object or nested object. To me it looks like you want a list... so you should replace the curly braces with square brackets. see below

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"]
}

This is what you might have been trying to do... so look over the output and see if this is what you intended.

def covers(param1):
    r_list = []
    for k,v in COURSES.items():
        if param1 in v:
            r_list.append(k)
    return r_list

def covers_all(single_set):
    new_list = []
    for key, value in COURSES.items():
        if single_set in key:
            new_list.append((key,value))
    return new_list


# maybe you want to do something like this?
print "Courses which cover 'strings'"
print covers("strings") # find which courses cover "strings"

print "What topics are in courses with 'Java' in the title"
print covers_all("Java") # find what topics are in any courses with "Java" in the title

output is:

Courses which cover 'strings'
['Python Basics', 'Ruby Basics', 'Java Basics', 'PHP Basics']
What topics are in courses with 'Java' in the title
[('Java Basics', ['Java', 'strings', 'variables', 'input', 'exceptions', 'integers', 'booleans', 'loops'])]
Douglas North
Douglas North
10,884 Points

Hey Jeff, thanks for the response :) The dictionary was provided and they wanted me to use these set functions. It took a 2 hour break and a tea but i found that there were more functions i didn't know about it works with =< instead of &.also

thank you