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 trialLeo Yun Tao
15,956 PointsHelp! what did I do wrong
I am currently stuck in Challenge Task 1 of 2, what did I do wrong with this code?
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(x):
array = []
sets = {}
#loop through the sets of topic
for topics in COURSES.values():
# loop through the topic
for topic in topics.copy():
# find the number of sets in the array
for find in x:
# if the set is in the topic
if(find in topic):
if find in COURSES["Java Basics"]:
array.append("Java Basics")
elif find in COURSES["PHP Basics"]:
array.append("PHP Basics")
elif find in COURSES["Ruby Basics"]:
array.append("Ruby Basics")
elif find in COURSES["Python Basics"]:
array.append("Python Basics")
sets = set(array)
array = list(sets)
return array
1 Answer
frankgenova
Python Web Development Techdegree Student 15,616 PointsCOURSES = {
"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_set):
course_has_topics_list = []
for course in COURSES:
# print("course: {}".format(course))
topics = COURSES[course]
# print("COURSES[{}]: {}".format(course, topics))
if topics.intersection(topic_set):
# print("topics intersects topic_set: {} {} \n{}".format(topics, topic_set, topics.intersection(topic_set)))
course_has_topics_list.append(course)
else:
continue
# print("no intersection between: {} {}".format(topics, topic_set))
return course_has_topics_list
print(covers({"Python"}))
frankgenova
Python Web Development Techdegree Student 15,616 Pointsfrankgenova
Python Web Development Techdegree Student 15,616 PointsTry the intersection method for sets.
SPOILER ALERT I have posted a complete solution below, but suggest you try this first.