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 trialKafe Hezam
11,070 PointsReturn the names of all of the courses, in a list by using set() function.
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(course): list_of_courses = [] for subject in COURSES.keys(): if course.intersection(COURSES[subject]): list_of_courses.append(subject) return list_of_courses print(covers({"conditions", "input"}))
def covers_all(topic): all_courses = [] for courses in covers(topic): if COURSES[courses].issuperset(topic): all_courses.append(courses) return all_courses print(covers_all({"condition", "input"}))
Hi all! :) I am trying to return the names of all the courses, in a list if every element of the parameter exists in the values of the object COURSES.
In the second function, I am iterating over the 1st function which returns a list of all the classes. But, the overall result is an empty list :(
Can anyone explains why it keeps executing an empty list?:)
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(course):
list_of_courses = []
for subject in COURSES.keys():
if course.intersection(COURSES[subject]):
list_of_courses.append(subject)
return list_of_courses
def covers_all(topic):
all_courses = []
for courses in covers(topic):
if COURSES[courses].issuperset(topic):
all_courses.append(courses)
return all_courses
1 Answer
Anthony Crespo
Python Web Development Techdegree Student 12,973 PointsYou forgot to Indent the block after your for loop in your covers_all function
def covers_all(topic):
all_courses = []
for courses in covers(topic):
if COURSES[courses].issuperset(topic):
all_courses.append(courses)
return all_courses