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

Isaac John
Isaac John
7,516 Points

Function not giving final answer

my_dict = {'a': 1, 'b':2, 'c':3}
my_list = ['a', 'b', 'd', 'e']

def members(my_dict, my_list):
    count = 0
    for key in my_dict:
        if key in my_list:
            count = count + 1
            return count

I keep getting 1 for the answer when it's supposed to be 2 for the test. However when I run it on workspaces I get the answer 1 followed by the answer 2 so I know my code is working somewhat. The problem is how to get the 2 and not 1.

1 Answer

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

Your return is inside your for loop, so it's not sending back the final count. You need to un-indent it so it's level with the for loop. That way it'll be run once the for loop finishes.