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 trial

Python Python Collections (Retired) Dictionaries Membership

Shadow Skillz
Shadow Skillz
3,020 Points

Write a function named members that takes two arguments, a dictionary and a list of keys. Return a count of how many of

Hey everyone i could you use some help I'm not to sure how to got about this is asking me where's the members but to be honest I'm lost anyones help would be greatly appreciated

counts.py
# 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 = {'apples': 1, 'orange': 2, 'pairs': 3, 'Plums': 4}
    my_list = ['cheese', 'milk' , 'plums', 'orange', 'tange']

    totsl = 0
    for x in my_list:
        if x in my_dict:
          total += 1
          return total

1 Answer

YI LI
YI LI
4,094 Points

Hello Christian, First, the answer don't need to include the details of my_dict and my_list, just take them as variables. Second, you misspell total to totsl. Third, we want to get the total count after we run through all the values in my_list, so return should be put to the same level as for loop.

Here is an example:

def members(my_dict, my_list):
  total = 0
  for x in my_list:
      if x in my_dict:
         total += 1
  return total
YI LI
YI LI
4,094 Points

Sometimes, I compile my code by workspaces to see what's wrong, because the challenge compiler doesn't tell us the error message of our code.

Shadow Skillz
Shadow Skillz
3,020 Points

Yi thanks a ton i really appreaciate the help