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 courses, in a list.
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
6 Answers
Chris Freeman
Treehouse Moderator 68,457 PointsYou are very close. If statement inside the for loop needs to be in dented further.
Kafe Hezam
11,070 PointsI had it dented it when I was executing it but I accidentally typed it wrong here. So, I still keep getting an empty list :)
Chris Freeman
Treehouse Moderator 68,457 PointsIt would depend on how you were executing the function covers_all. When I fix the indentation it passes the challenge.
Kafe Hezam
11,070 PointsThis is how my second function looks like:
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"}))
[MOD: added ```python formatting -cf]
Chris Freeman
Treehouse Moderator 68,457 Points# Using
print(covers_all({"condition", "input"}))
print(covers_all({"input"}))
print(covers_all({"condition"}))
# returns
[]
[‘Ruby Basics’, ‘Python Basics’, ‘Java Basics’]
[]
Can you see why?
Kafe Hezam
11,070 Pointsdef 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"}))
Kafe Hezam
11,070 PointsSorry, for keep posting the same thing with the wrong formats. I am just trying to show that the indentation of if statement is dented to the right as well as the append function.
Chris Freeman
Treehouse Moderator 68,457 PointsYou can apply formatting by reviewing the markdown cheatsheet link below the text box.
Kafe Hezam
11,070 PointsI can see why is because "condition" should be "conditions". But, still keep getting an empty list :(
Anyways, thank you so much for all the help, Chris!:)
Chris Freeman
Treehouse Moderator 68,457 Points# using
print(covers_all({"condition", "input"}))
print(covers_all({"input"}))
print(covers_all({"condition"}))
# corrected typo
print(covers_all({"conditions", "input"}))
# returns
[]
[‘Ruby Basics’, ‘Python Basics’, ‘Java Basics’]
[]
[‘Ruby Basics’, ‘Python Basics’]