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 trialAshley Hu
5,799 PointsNot sure where to begin
Hey, it says that i is not defined but i dont know where to go from here
# 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(dic, keys):
count = 0
for i in dic[i]:
if i in keys:
count = count+1
return count
1 Answer
andren
28,558 PointsThere are two errors in your code, the first is that you have slightly misunderstood how for loop work, the structure of a for loop is this:
for variable in list
The thing following the in keyword is just the list the for loop should loop over, you don't need to extract individual items out from the list using brackets like you are doing, pulling the items out of the list is something the for loop does automatically.
The second issue is that your return statement is within the if statement within the for loop, this means that you will run the return statement during the first run of the for loop. Since return causes the function to stop and immediately returns the value, it means that the for loop will never run more than once.
So you have to move the return statement outside of the for loop.
This is the code that after fixing those two issues:
def members(dic, keys):
count = 0
for i in dic:
if i in keys:
count = count+1
return count
And that code will pass the challenge.