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

Alex Meier
Alex Meier
885 Points

Challenge returning output from video--not what I set as the inputs

I'm really confused about the output I'm getting for this challenge. I ran the attached code but had the last line as:

return len(common)

When I did, the prompt said that the code returned 1 when it expected 2. I couldn't figure out what the problem was, so I replaced len(common) with 'return my_list'.

The weird thing is that it returns stuff from the exercise--not what I defined as my_list. As provided, I would expected the code to return apples, coconuts, grapes, strawberries because even if my code is wrong, if I define my_list as something, don't do anything to it, then ask the output to be my_list, it should return what I input. Instead, it appears to give what my_list was defined as during the previous video.

This is the error message I get: "Bummer, Expected 2, got {married': True, 'first_name': 'Kenneth', 'age': 33, 'last_name': 'Love', 'kids': True, 'retired': False}."

Can someone please explain why this is happening? Thanks!

counts.py
my_dict = {'apples': 1, 'bananas': 2, 'coconuts': 3}
my_list = ['apples', 'coconuts', 'grapes', 'strawberries']
def members(my_dict, my_list):
  common = []
  for key in my_dict: 
    if key in my_dict:
      common.append(key)
    return my_dict

### Example
# my_dict = {'apples': 1, 'bananas': 2, 'coconuts': 3}
# my_list = ['apples', 'coconuts', 'grapes', 'strawberries']
# members(my_dict, my_list) => 2
Alex Meier
Alex Meier
885 Points

There's an error in my post. This sentence "The weird thing is that it returns stuff from the exercise--not what I defined as my_list" mistakenly identifies the output as my_list rather than my_dict.

2 Answers

Andrew K
Andrew K
13,774 Points

Hi Alex, I assume that the Treehouse backend has a set of variables used to check the members function (or any of these code challenges) that is separate from the variables we put in. Since the function you wrote should be able to take any two list and dict arguments (not just the one you define), what you're getting in the error message is the arguments Treehouse puts in to check your code. They're not using your list and dict to check the function.

Think of it this way: you don't actually call the function yourself, right? But when you click the submit button, the function is called behind the scenes with the arguments Treehouse has already prepared inputted.

Andrew K
Andrew K
13,774 Points

Oh, and by the way; you were right to return len(common) before--that's not the issue with your code! Notice how you're checking your dict against your dict.

Alex Meier
Alex Meier
885 Points

Thanks, I realized that's what was happening when I changed the number of matches in my definitions for my_dict and my_list but kept getting 'expected 2' even if my list had 1 or 3+ common values. Figured out it was an indentation error. Thank you for your help.