473,396 Members | 1,866 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

keyword checker - keyword.kwlist

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
6 3327
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Hank Reed | last post by:
I am trying to use the spell checker on an unbound control in Access 2000. I run the checker in the AfterUpdate event of the control. After the spell checker is done, I get the following message:...
8
by: Joe | last post by:
Hello All: Does anyone know of a spell checker that works with .NET? Any options will be welcome. TIA, -- Joe
0
by: Pager O Rama | last post by:
Hi, R U BLOCKED ON MSN Messenger By Your Friend,then What are you waiting for.Visit one of the link below to find out who has blocked you in his/her friend list. http://www.hotmailpk.com/...
5
by: rob | last post by:
Does anyone know of a duplicate file checker project in C#? Couldn't locate anything on CodeProject or SourceForge. Has anyone here considered writing one?
13
by: kolmogolov | last post by:
/* Hi, I have removed things irrelevant to reproducing the problem. What's wrong with my code? Thanks in advance for any hint! 1166425362 */ #include <stdio.h> #include <stdlib.h> #include...
4
by: sweetguy1only | last post by:
Hi all, I am a MS Access developer using VB 6 (yes, I know it is a bit old). The problem I am having is, I have a software that allows my customers to put in the information of their clients....
6
by: Neil | last post by:
Is there way to have control over the MS-Access spell checking (besides just launching it)? We want to tell it to check all records, but skip certain fields (or, alternatively, ONLY check certain...
9
by: ARC | last post by:
Hello all, I developed a tool a year or so ago for adding your own spell-checker to an access application. This is mainly for those using the runtime, as you can't distribute the spell-checker...
3
by: Mike | last post by:
I have an app running at a client where, when the spell checker is supposed to run, it reports "Can't start spell checker because it is not installed". I have never had this before - it works...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.