By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,345 Members | 1,790 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,345 IT Pros & Developers. It's quick & easy.

keyword checker - keyword.kwlist

P: n/a
tom
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 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
On 10 Mag, 15:38, t...@finland.com 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 '07 #2

P: n/a
tom
>
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 '07 #3

P: n/a
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.

May 10 '07 #4

P: n/a
In article <_e***************@read3.inet.fi>, <to*@finland.comwrote:
[ ... ]
>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 -- si***@chiark.greenend.org.uk -- 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
May 10 '07 #5

P: n/a
tom
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 '07 #6

P: n/a
to*@finland.com 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
May 10 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.