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 trialCarlos Marin
8,009 PointsConfusion with the output.
Let's write some functions to explore set math a bit more. We're going to be using this COURSES dict in all of the examples. Don't change it, though!
So, first, write a function named covers that accepts a single parameter, a set of topics. Have the function return a list of courses from COURSES where the supplied set and the course's value (also a set) overlap.
For example, covers({"Python"}) would return ["Python Basics"].
I am putting this code into my interpreter, and all is working as indicated, but the test will not pass, it says "Didn't get the correct output."
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(arg):
list_of_courses = []
for course, values_of_course in COURSES.items():
if arg.issubset(values_of_course):
list_of_courses.append(course)
return list_of_courses
3 Answers
hector alvarado
15,796 PointsThe problem is you have a set inside a dict, sets cant be accessed as usual dict methods: https://www.programiz.com/python-programming/set
There is a fellow student who talk about this in his answer: https://teamtreehouse.com/community/not-really-sure-how-to-complete-this-challenge-please-help
Carlos Marin
8,009 PointsI get the same result from this code:
def covers(arg):
list_of_courses = []
for course in COURSES:
if arg.issubset(COURSES[course]):
list_of_courses.append(course)
return list_of_courses
Carlos Marin
8,009 PointsThis code worked. but I am wondering to as of why my last entries did not.
def covers(arg):
list_of_courses = []
for course in COURSES:
if arg & COURSES[course]:
list_of_courses.append(course)
return list_of_courses
Dave StSomeWhere
19,870 PointsThe issubset() method is similar to the <= operator
While the intersection() method is similar to the & operator.
You changed from subset to intersection, is why this code worked.
Carlos Marin
8,009 Points@Dave The 2 functions I provided both return ['Python Basics'] when passed; arg = {"Python"} which is what the challenge is asking me to do. I understand how the & operator works. I am wondering why my two functions do not qualify. I have 2 different sources that tell me .issubset() is checking if the first set is inside of the 2nd set
"issubset(other)
set <= other
Test whether every element in the set is in other."
Scources: 1) https://docs.python.org/3/library/stdtypes.html#set 2) https://realpython.com/python-sets/
Dave StSomeWhere
19,870 PointsUnderstood, the challenge is looking for the intersection (overlap) and not a subset.
So with subset print(covers({"Python", "foo"}))
will return nothing []
While intersection will return - ['Python Basics']
Carlos Marin
8,009 PointsCarlos Marin
8,009 PointsInteresting, I see other people using the same method as I am trying to deploy, but they are saying that their code has passed. When I run it, it no longer gets passed. fortunately Dave, StSomeWhere helped me figure it out! :)