keyword checker - keyword.kwlist

T

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"
 
A

alessiogiovanni.baroni

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...
 
T

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.
 
S

Sion Arrowsmith

[QUOTE= said:
Try to change the 'input' variable name with other...
Changed input variable to myInput, but the result is still the same.[/QUOTE]

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(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.
?
.... 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 keywordEnter identifier to check >> else
else is keyword
 
S

Steven D'Aprano

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.
 
T

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
 
P

Peter Otten

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top