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 trialTyler Shand
Python Web Development Techdegree Student 15,043 PointsPython Collections - Set Math Code Challenge Not Passing
I copied the code challenge to my computer and covers
returns a list of courses from COURSES that have overlapping sets. However, when I run the same code on the code challenge I get this: "Didn't get the right output from covers
."
What am I doing wrong?
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):
courses_covered = []
for course in COURSES:
if COURSES[course] & (topic) == topic:
courses_covered += (course)
return courses_covered
6 Answers
Gleb Sklyr
17,492 PointsBen Schroeder was right using the .keys() function Also mind that keys() returns a list so I converted it to a set to use intersection() There is the logic in my code(below):
- for every course name
- if the intersection of the argument and the set in COURSES with that name yield a list of 1 item at least (meaning there is overlap in between the topics argument and the topics covered in the course)
- then append the course name to the list
def covers(topic):
courses_on_topic = []
for course_name in set(COURSES.keys()):
if len(topic.intersection(COURSES[course_name])) > 0:
courses_on_topic.append(course_name)
return courses_on_topic
- To modify this code to pass the next challenge you only need to make a small change!!!
Ben Schroeder
22,818 PointsInstead of "for course in COURSES," try this:
for course in COURSES.keys():
Tyler Shand
Python Web Development Techdegree Student 15,043 PointsI tried adding that but I'm still getting the same error message.
def covers(topic):
courses_covered = []
for course in COURSES.keys():
if COURSES[course] & topic == topic:
courses_covered += course
return courses_covered
Jorge Gimeno
5,956 PointsInstead of adding to the list, try APPENDing to it.
David Hortensius
3,898 PointsHi, I tried similar things and the test just doesn't pass. Can somebody help out?
def covers(topic_set):
matching_courses = []
for course in COURSES:
#if not topic_set.difference(COURSES[course]): ## version 1
if topic_set & COURSES[course] == topic_set: ## version 2, both work in Workspaces
matching_courses.append(course)
return matching_courses
Mauro Y.
6,520 PointsThis worked in Workspaces, but doesn't pass too. Why?
def covers(subject):
result = []
for course_set in COURSES:
if subject.issubset(COURSES[course_set]):
result.append(course_set)
return result
Ismail KOÇ
1,748 Pointsif you have trouble with second step, check out this link
austinnoh
1,588 Pointsaustinnoh
1,588 PointsThe code works but I have a question. Would covers({"Python"},{"functions")) return ["Python Basics"] or ["Python Basics", "Python Basics"]...? I am genuinely curious