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 trialAshley Keeling
11,476 Pointscan someone help me with sets and this function please
I am not sure why it isn't working and I don't know if I am missing anything from it thanks
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(st):
sets=[]
for key in COURSES.items():
if key in st & COURSES
sets.append(value)
return sets
2 Answers
Kip Yin
4,847 Points- You missed a colon in the
if
statement:
for key in COURSES.items():
if key in st & COURSES:
sets.append(value)
- The
value
variable is not defined anywhere. I'm guessingvalue
is referring to the values inCOURSES
. If so, try:
for key, value in COURSES.items():
# do stuff
-
COURSES
is a dictionary, not a set. That's why&
operator is not working here. Try:
for key, value in COURSES.items():
if value & st:
sets.append(key)
Since you are already iterating through the keys and the items in COURSES
, you only need to check if the value
and the input st
intersect.
- Lastly, the
sets
variable in your code is actually alist
type. This can be very confusing when someone else is reading your code.
Stuart McIntosh
Python Web Development Techdegree Graduate 22,874 Pointshi there, I am unsure if the way I did this challenge was the best way, however, here are some pointers that may help.
- firstly you want to create a list to return
- then you want to iterate through courses and get the keys and values - the value in Courses is a set
- test your passed in set for an intersection with the vaiue from Courses i.e. use &
- if that test passes i.e. you have found an intersection then append the key of that key, value pair into the list you created
- return the list
def covers(topics):
course_list = []
for key, value in COURSES.items():
# test for an intersection
# if True the add the key to the list
A clue for the next challenge is to use an intersection test again but also check the length of that intersection against the length of the passed in set
Hope that helps...
Ashley Keeling
11,476 Pointsthanks
Ashley Keeling
11,476 PointsAshley Keeling
11,476 Pointsthanks I did the challenge