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

Can't solve counts.py Code Challenge

I'm getting the error message "Bummer! Expected 1, got 1" error message on my Code Challenge.

The Challenge is:

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

Here's my code:

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

2 Answers

Milo Winningham
seal-mask
.a{fill-rule:evenodd;}techdegree
Milo Winningham
Web Development Techdegree Student 3,317 Points

count =+ 1 will set the count to the number +1. To increment the count variable you'd need to use count += 1. We should get a better error message for this though.

Jose Luis Lopez
Jose Luis Lopez
19,179 Points

def members(dictionary, keys): count = 0 for key in dictionary: if key in keys: count =+2 return count

This work for me

Dan Badertscher
Dan Badertscher
5,327 Points

This worked for me as well. Can you please explain why we are using "count =+2" instead of "count=+1"?

Yu Cao
Yu Cao
2,414 Points

You are doing it wrong. Because the example that was used to check for this question happened to yield 2. So when you use count=+2, it only returns a hardcoded number of 2. When you use it for another example that has 3 keys in the list, you are still getting 2 and that will be wrong. Milo's comment is correct and you should use count += 1 to increment your count by 1 for each key in the list. Your answer is definitely wrong and just happened to be the correct answer for this particular answer.