0

The basic gist of the program is to start with a list of employee names, then sort it. Wait for user to input "end" to stop populating the list of names (I have 100 names, I cut it short for the example). Afterwards, the user can enter an employee name and the program will run difflib.get_close_matches().

Here's the question; I'm getting a syntax error for get_close_matches. How should I be entering the difflib differently? Also; if you have any tips for making the code more efficient, please also state how and why it's more efficient. I'm fairly inexperienced with Python, so be gentle, eh?

EXAMPLE CODE:

import difflib
employeeNames = ['Colton','Jayne','Barb','Carlene','Dick','Despina']
employeeNames.sort()
endInput = input('Type "end" to view list of names.\n\n')
if endInput == "end":
    userEmpName = input("Please enter the employee name you're searching for. We'll return the best match on record."
get_close_matches(userEmpName, employeeNames, 1)
user3259628
  • 5
  • 1
  • 5

1 Answers1

0

Your code has syntax errors: Match this code with yours:

import difflib
employeeNames = ['Colton','Jayne','Barb','Carlene','Dick','Despina']
employeeNames.sort()
endInput = raw_input('Type "end" to view list of names.\n\n')
if endInput == "end":
    userEmpName = raw_input("Please enter the employee name you're searching for. We'll return the best match on record.")
    print difflib.get_close_matches(userEmpName, employeeNames, 1)
  1. you didn't close the open brace in input() method.

  2. I suggest using raw_input() instead of using input() while dealing with strings.

  3. you should use the classname.method() if you have imported only the class (in your case import difflib) so use difflib.get_close_matches(string,list,n) instead.

  4. You need to use print statement before the returned value.

Also get_close_matches() should be called inside of if because if endInput!='end' then NameError will occur for userEmpName.

EDIT:

I should have asked you about your python interpreter version.
The print line should use braces like this.


print(difflib.get_close_matches(userEmpName, employeeNames, 1))

The reason is in python 2.x print is a statement(as I mentioned in 4rth point) but in python 3.x its a function.

noobmaster69
  • 2,396
  • 25
  • 40
  • I tried to run your code, I get the same error message as I did in mine; invalid syntax. It's highlighting "difflib" after print. Also; thanks for the advice. I'll try to incorporate that as I can. :) – user3259628 Feb 01 '14 at 07:29
  • I'm away from my computer until I get back from work, but I'll run it again when I come home and let you know the outcome. – user3259628 Feb 01 '14 at 11:24
  • I don't seem to have the same IDLE editor as you; `print` doesn't turn orange, and `raw_input` doesn't turn purple. Here's what I get when I run it: [link](http://i162.photobucket.com/albums/t280/gorak_01/difflibError_zps233fec06.png) – user3259628 Feb 02 '14 at 05:05
  • And don't worry about the differences of `print` its only because I am using python 2.7 and you are using python 3.3 – noobmaster69 Feb 02 '14 at 05:57
  • Hey hey! It's workin' like a charm now. Thanks for the help with the code, and the explanation as to why the syntax error kept popping up. I'll make sure to remember this for later. – user3259628 Feb 02 '14 at 06:00
  • I'm adding a bit to the code where if the GCM name isn't the name of the person they wanted, then discard the closest match and re-run the function and call the second-closest match (now first, since the function would throw out the first match). – user3259628 Feb 02 '14 at 07:11
  • Do you have any comments on if this can be written better? Here's what I have so far: `def throwout(pickedName):` `employeeNames.remove(pickedName)` `pickedName = difflib.get_close_matches(userEmpName, employeeNames, 1)` ` print(pickedName)` ` userFirstInput = input("Is this the first name of the person you're looking for? Type 'Yes' or 'No'.")` – user3259628 Feb 02 '14 at 07:17
  • you can ask a separate question...and link me there...this looks messy to read – noobmaster69 Feb 02 '14 at 07:22
  • Good point, here it is: [new thread](http://stackoverflow.com/questions/21517842/difflib-get-close-matches-throw-out-names-in-a-list-if-first-answer-isnt-correc) – user3259628 Feb 02 '14 at 23:49