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 trialInes Fazlić
Python Web Development Techdegree Student 9,569 Pointsset math, don't understand what's wrong with my code
so this is the question: 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"].
Can please anyone tell me what's wrong with my code? I don't get it..
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(set):
list: []
for key, value in COURSES.items():
if set.intersection(value):
list.append(key)
return list
1 Answer
Chris Howell
Python Web Development Techdegree Graduate 49,702 PointsHey Ines Fazlić
So it looks like you already figured it out, but I am going to elaborate a little more just in case anyone finds your post and runs into the same problem that way your post can have an answer on it. :-)
:
in Python are generally the indicator for a block of code you are about to begin.
Blocks of code can be:
- Conditional statements like
if
orelse
- Loops like
for
orwhile
- Functions definitions like
def
- Class definitions like
class
These all have a :
proceeding them at the end of the line so that Python knows the indented lines beneath them belong inside that block.
Also just a side note about your code below:
def covers(set):
list: [] # the : here is the problem
for key, value in COURSES.items():
if set.intersection(value):
list.append(key)
return list
Be careful using variable names that are also built-in function/class names or keywords in Python.
Here is link to listing of some Python built-ins
If you set a variable name to the same name as anyone of those, you can overwrite the meaning and then wont be able to use it later how you wanted to. So like below you are passing in set
into your def covers(set)
. This may change the way set()
normally behaves especially if you pass in something that isnt a set you will change the built-in to something else entirely inside that function. If that makes sense.
Ines Fazlić
Python Web Development Techdegree Student 9,569 PointsInes Fazlić
Python Web Development Techdegree Student 9,569 PointsAh got it! always trouble with colons lol