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 trialJason Smith
8,668 Pointsit says i didn't get the right output. what am i doing wrong?
i don't really see why this wouldn't work
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 (topic):
return list(topic.intersection(COURSES))
1 Answer
Alex Koumparos
Python Development Techdegree Student 36,887 PointsHi Jason,
Let's run your code in the Python interpreter and see what it tells us:
>>> covers({"Python"})
[]
So we're getting an empty list, but the question expects:
For example, covers({"Python"}) would return ["Python Basics"].
Ignoring the type conversion from set to list, your function only has one method call, so it's a pretty safe bet that the issue is in there somewhere.
Let's take a quick look at the official docs for intersection
:
intersection(*others)
set & other & ...
Return a new set with elements common to the set and all others.
... Note, the non-operator versions ofunion()
,intersection()
,difference()
, andsymmetric_difference()
,issubset()
, andissuperset()
methods will accept any iterable as an argument.
So, this tells us that the argument to intersection
needs to be an iterable, but it can be any iterable.
We know that we're providing an iterable, otherwise our code would do this in the interpreter:
>>> {"Python"}.intersection(3) # an int is not an iterable
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'int' object is not iterable
Since we get an empty list but not a TypeError, we can deduce that we are passing in an iterable, but not the iterable we expect.
What about if we do this:
>>> {"Python"}.intersection({"Python", "functions", "variables", "booleans", "integers", "floats", "arrays", "strings", "exceptions", "conditions", "input", "loops"})
{'Python'}
That looks pretty promising. But if we do this:
>>> {"Python"}.intersection({"Python Basics": {"Python", "functions", "variables", "booleans", "integers", "floats", "arrays", "strings", "exceptions", "conditions", "input", "loops"}})
set()
We get an empty set. What we are seeing is that intersection
will only look at each element of the iterable you pass in and compare that to your set. Since the elements of promising version were the topics in the set, it could match "Python"
to "Python"
. But in the dictionary, the elements are not topics, they are dictionary elements, i.e., a key-value pair. In the specific case above, the element is {"Python Basics": {"Python", "functions", etc}}
(Strictly, when you pass in the dictionary, intersection()
treats the key as the element for comparing to your set, so in this case, just "Python Basics").
Hopefully, this clears up where you are going off-track and gives you some hints for how to proceed. If you need any more hints, just let me know.
Cheers
Alex