字符串标识符首先要以字母或者下划线开始,后面要跟字母,下划线或者数字
编写python脚本:使之可以检测长度大于等于1的标识符,并且可以识别 Python 关键字
import keyword
alphas = string.letters + '_'
nums = string.digits
case = keyword.kwlist
print 'Welcome to the Identifier Checker v1.0'
alphnums=alphas + nums
isKeyword=False
isIdentifier=False
myInput = raw_input('Identifier to test? ')
if len(myInput) >= 1:
if myInput[0] not in alphas:
print '''invalid: first symbol must be'''
print alphas
isIdentifer=False
else:
if len(myInput) == 1:
isIdentifer=True
else:
for otherChar in myInput[1:]:
if otherChar not in alphnums:
print '''invalid: remaining
symbols must be'''
print alphnums
isIdentifier=False
break
else:
isIdentifier=True
if myInput in case:
print '"%s" is a keyword of Python' % myInput
isKeyword=True
if isIdentifier and (not isKeyword):
print 'okay as an identifier'