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 trialJerry Peng
8,608 PointsTotally no idea how to do this task
Totally lost ...no idea how to do this task
# 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
5 Answers
Chris Howell
Python Web Development Techdegree Graduate 49,702 PointsIt wants you to:
Write a function named members that takes two arguments, a dictionary and a list of keys. Return a count of how many of the items in the list are also keys in the dictionary.
So you will want to start with writing a function named members.
If you aren't sure how to write a function or haven't covered functions yet.
Go have a look at this course: Python Basics
If you just simply forgot, you can re-cover that material here: Functions and Objects
and if you get your own code written and it still wont pass or you still are stuck, post your code and we can try and help explain why its not passing or where to look for any errors.
Chris Howell
Python Web Development Techdegree Graduate 49,702 PointsI think I see whats going on.
So the error you are getting is a KeyError, which keys are part of a Dict. You are getting an error because gender does not exist in the Dictionary so you must Check for Membership. Which basically means you have to make sure you check that a given key exists in that dictionary before you try to call a dictionary by a key that may not exist.
As you will see in the example comments for the challenge.
### Example
# my_dict = {'apples': 1, 'bananas': 2, 'coconuts': 3}
# my_list = ['apples', 'coconuts', 'grapes', 'strawberries']
# members(my_dict, my_list) => 2
The LIST has 1 item that the DICT does not.
Checking for membership
# assuming some_key has something in it
# assuming some_dict is a dictionary with items in it.
if some_key in some_dict: # in - is checking for membership
# it exists so we can get or do something with value stored in that key.
else:
# it didnt exist
akhilesh Bhartiya
1,048 Pointsdef members(my_dict, my_list):
count = 0
for temp in my_list:
try:
if my_dict[temp] == 1 or my_dict[temp] == 2 or my_dict[temp] == 3:
continue
except KeyError:
pass
else:
count += 1
return count
akhilesh Bhartiya
1,048 Pointsdef members(my_dict, my_list):
count = 0
for temp in my_list:
try:
if my_dict[temp] == 1 or my_dict[temp] == 2 or my_dict[temp] == 3:
count += 1
except KeyError:
pass
return count
Mariana Hoffmann
Courses Plus Student 11,046 PointsHello Jerry,
I'm pretty sure it's not the right way to answer the challenge, but it was the only way I could get it done.
So, here it is:
# 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):
count = 2
for temp in my_list:
if my_dict in my_list:
count += 2
else:
return count
akhilesh Bhartiya
1,048 Pointsyou initialised count 2 thats why its returning 2 otherwise it has nothing to do with your for loop or the if statement
Jerry Peng
8,608 PointsJerry Peng
8,608 PointsDear Chris,
Thanks for your reply. The following is my code:
def members(my_dict, my_list):
I'm getting a "gender" error. Please let me know what I'm doing wrong. Appreciate.