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 trialBozhong Tao
18,365 Pointscovers({"Python"}) would return ["Python Basics"] would not work for this code, however it worked in my atom console@@
I get the "Bummer! Didn't get the right output from covers
." prompt using the follow code:
def covers(arg):
subject = []
for key, value in COURSES.items():
if value & arg == arg:
subject.append(key)
return subject
If I replace the content in the if statement(replace "if value & arg == arg:" into "if value & arg:"), I can pass the first task, however I can print out the correct output in my atom console using the code block above.
def covers(arg):
subject = []
for key, value in COURSES.items():
if value & arg:
subject.append(key)
return subject
output = covers({"Python"})
print(output)
I wonder why is that? is there anything specific I should be aware of when comparing intersection result using '==' or it's a bug?
Thanks for checking out!
3 Answers
Bill Leddy
2,720 PointsThe & intersection operator returns a set (or None). If the set returned is only a partial match for arg your append does not execute.
Bozhong Tao
18,365 PointsThanks for the info Bill :) However, the code block above can print out ["Python Basics"] in my atom console, however it will fail the task check unless I replace the if statement into
if value & arg:
So why "if value & arg:" work and "if value & arg == arg" would fail?
Bill Leddy
2,720 PointsI think whatβs going on is that in the case of an intersection (&) only some of the items in the args set may be returned. If there are 3 items in args the intersection may return only 1 or 2 of them. That still means that you want the course in your final output but the 2 items in the returned set will not exactly match args.
BTW I solved it ( eventually) with a differen approach but your solution was cleaner.
Bozhong Tao
18,365 PointsHey Bill, thanks again for the response :) That make sense, I assumed the arg used by the task checker would only contain 1 item(In fact it could contain many) . that explains why the "if value & arg == arg" would fail. If the arg = {"Python", "CSS"} then the if statement would be False. Cheers!
PS.. What is your 'different approach'? Am interested :)