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

I'm stuck. Any idea if I'm using the wrong syntax for the try/except blocks and/or referencing list items?

Here is my code.

I appreciate the help in advance if any is given.

I'm not trying to have someone solve it for me just point me in the right direction as I can't use Eclipse for debugging or code completion as my crutch due to the laptop I'm using.

def members(a_dict, a_list):
  num = 0;
  index = 0;
  while index <= len(a_list):
    try:
      keyword = a_list(index);
      dic_key = a_dict[keyword];
      if len(dic_key) > 0:
        num = num + 1;
    except:
      keyword = a_list(index);
      print("{} does not exist.".format(keyword));
    index = index + 1;
  return num;

1 Answer

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

Couple of things.

  1. you can just loop through a list, getting each item of the list. for x in y:, where x is what you want to call a thing in the list, and y is the list. For example: for book in library: No reason to have an index or anything.
  2. read the comments I put into the starter file. There's an easier way to see if a key belongs to a dictionary than using try/except.
  3. why are you calling a_list like it's a function?

Also, why all the semicolons? They won't hurt anything, but they're definitely not needed in Python.