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 trialAndy Christie
1,361 PointsI'm really having trouble with this python code, please help.
Hi I have to check to see if there are any matching items in a list and a dictionary but I'm really having trouble please can you help.
# 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
my_list = ["app", "ora", "cac"]
my_dict = {"app": 1, "ora": 2, "em": 3}
def members(my_list, my_dict):
count = int()
for vart in my_list:
if vart in my_dict:
count + 1
return count()
1 Answer
Umair Shaikh
12,727 PointsMake some changes in your code
count = 0
count += 1
return count
my_list = ["app", "ora", "cac"] my_dict = {"app": 1, "ora": 2, "em": 3}
def members(my_list, my_dict): count = 0 for vart in my_list: if vart in my_dict: count += 1 return count
print(members(my_list, my_dict))
Andy Christie
1,361 PointsAndy Christie
1,361 PointsHi yes, thankyou I just noticed that before you replied but now it just says try again unless I indent the return count in to the if loop then it just says says again.