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 trialGuy-Francis Kono
9,289 PointsNeed Help!
How do I get this completed? I am thoroughly confused! And I can't seem to find any hints in the videos.
# You can check for dictionary membership using the
# "key in dict" syntax from lists.
### Example
# my_dict = {'apples': 1, 'bananas': 2, 'coconuts': 3}
# my_list = ['apples', 'coconuts', 'grapes', 'strawberries']
# members(my_dict, my_list) => 2
def members(my_dict, my_list):
my_dict = {}
my_list = []
for key in my_list:
if key in my_dict:
return key, my_list.count(key)
2 Answers
Jennifer Nordell
Treehouse TeacherHi! It seems to me that you're very close here, but there's a couple of problems. So, I'm going to give you some hints. First, and foremost, you're overwriting what Treehouse is sending in by resetting the my_dict
and my_list
to empty. Everything they just sent in has been erased.
The second problem is in what you're returning. They want you to return a count of how many keys are found in the dictionary. This will require you to set up a variable to keep count. At the end, you are to return the count.
Hope this helps!
Russell Sawyer
Front End Web Development Techdegree Student 15,705 PointsYou are pretty close. Remember indentation is important. Where you have your return statement would return a value every time the loop was run. You want to return a value after the for loop runs. Also, you can use all of the internet to find the answer.
def members(my_dict, my_list):
count = 0
for key in my_dict:
if key in my_list:
count += 1
return count
Guy-Francis Kono
9,289 PointsThanks for the help Russell. It came in valuable to compare my final answer with what you shared with me.
Guy-Francis Kono
9,289 PointsGuy-Francis Kono
9,289 PointsThank you for the explanation, Jennifer. That clarified the whole process to me so I could work on getting the answer.