keyword checker - keyword.kwlist

Discussion in 'Python' started by tom@finland.com, May 10, 2007.

  1. Guest

    Hi

    I try to check whether a given input is keyword or not. However this
    script won't identify keyword input as a keyword. How should I modify it
    to make it work?

    #!usr/bin/env python
    import keyword

    input = raw_input('Enter identifier to check >> ')
    if input in keyword.kwlist:
    print input + "is keyword"

    else:
    print input + "is not keyword"
    , May 10, 2007
    #1
    1. Advertising

  2. Guest

    On 10 Mag, 15:38, wrote:
    > Hi
    >
    > I try to check whether a given input is keyword or not. However this
    > script won't identify keyword input as a keyword. How should I modify it
    > to make it work?
    >
    > #!usr/bin/env python
    > import keyword
    >
    > input = raw_input('Enter identifier to check >> ')
    > if input in keyword.kwlist:
    > print input + "is keyword"
    >
    > else:
    > print input + "is not keyword"


    Hmm... I tried, and identify it.
    Try to change the 'input' variable name with other...
    , May 10, 2007
    #2
    1. Advertising

  3. Guest

    >
    > Hmm... I tried, and identify it.
    > Try to change the 'input' variable name with other...
    >

    Changed input variable to myInput, but the result is still the same.

    for example, 'else' isn't identified as a keyword by the script though
    it exists in keyword.kwlist.
    , May 10, 2007
    #3
  4. In article <_eG0i.142$>, <> wrote:
    [ ... ]
    >> Try to change the 'input' variable name with other...

    >Changed input variable to myInput, but the result is still the same.


    That was good advice, but isn't going to help here. Because "input"
    isn't a keyword, it's a builtin. If you want to check builtins as
    well as keywords, you need
    >>> if myInput in keyword.kwlist + dir(__builtins__):

    (Although obviously you'd pre-build that list.)

    >for example, 'else' isn't identified as a keyword by the script though
    >it exists in keyword.kwlist.


    ?
    >>> def check():

    .... input = raw_input('Enter identifier to check >> ')
    .... if input in keyword.kwlist:
    .... print input, "is keyword"
    .... else:
    .... print input, "is not keyword"
    ....
    Enter identifier to check >> input
    input is not keyword
    >>> check()

    Enter identifier to check >> else
    else is keyword
    >>>


    --
    \S -- -- http://www.chaos.org.uk/~sion/
    "Frankly I have no feelings towards penguins one way or the other"
    -- Arthur C. Clarke
    her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
    Sion Arrowsmith, May 10, 2007
    #4
  5. On Thu, 10 May 2007 13:38:40 +0000, tom wrote:

    > Hi
    >
    > I try to check whether a given input is keyword or not. However this
    > script won't identify keyword input as a keyword. How should I modify it
    > to make it work?


    It works for me.

    Try printing keyword.__file__ to make sure you are importing the right
    file. Also try printing keyword.kwlist.

    --
    Steven.
    Steven D'Aprano, May 10, 2007
    #5
  6. Guest

    Still no go. I just can't get it right. My current script:

    #!usr/bin/env python
    import keyword

    myInput = raw_input('Enter identifier to check >> ')
    if myInput in keyword.kwlist:
    print myInput, "is keyword"

    else:
    print myInput, "is not keyword"

    print keyword.kwlist
    print keyword.__file__

    And the output:

    Enter identifier to check >> else
    else
    is not keyword
    ['and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del',
    'elif', 'else', 'except', 'exec', 'finally', 'for', 'from', 'global',
    'if', 'import', 'in', 'is', 'lambda', 'not', 'or', 'pass', 'print',
    'raise', 'return', 'try', 'while', 'with', 'yield']
    F:\Ohjelmat\Python25\Lib\keyword.pyc
    , May 10, 2007
    #6
  7. Peter Otten Guest

    wrote:

    > Still no go. I just can't get it right. My current script:
    >
    > #!usr/bin/env python
    > import keyword
    >
    > myInput = raw_input('Enter identifier to check >> ')
    > if myInput in keyword.kwlist:
    > print myInput, "is keyword"
    >
    > else:
    > print myInput, "is not keyword"
    >
    > print keyword.kwlist
    > print keyword.__file__
    >
    > And the output:
    >
    > Enter identifier to check >> else
    > else
    > is not keyword
    > ['and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del',
    > 'elif', 'else', 'except', 'exec', 'finally', 'for', 'from', 'global',
    > 'if', 'import', 'in', 'is', 'lambda', 'not', 'or', 'pass', 'print',
    > 'raise', 'return', 'try', 'while', 'with', 'yield']
    > F:\Ohjelmat\Python25\Lib\keyword.pyc


    Does

    myInput = raw_input("...").strip()
    # ...

    work?

    Your raw_input() seems to be broken and includes the trailing newline.

    Peter
    Peter Otten, May 10, 2007
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Max
    Replies:
    6
    Views:
    3,365
  2. pablo pecora

    Aspx permission checker

    pablo pecora, Nov 17, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    471
    Natty Gur
    Nov 18, 2003
  3. Hamilton, William

    RE: keyword checker - keyword.kwlist

    Hamilton, William, May 10, 2007, in forum: Python
    Replies:
    4
    Views:
    349
  4. Pager O Rama

    MSN BLOCK CHECKER-MSN STATUS CHECKER-MSN PROBLEMS

    Pager O Rama, Apr 4, 2006, in forum: ASP General
    Replies:
    0
    Views:
    235
    Pager O Rama
    Apr 4, 2006
  5. Jacob Grover
    Replies:
    5
    Views:
    308
    Jacob Grover
    Jul 18, 2008
Loading...

Share This Page