有这样一道题目: 字符串标识符.修改例 6-1 的 idcheck.py 脚本,使之可以检测长度为一的标识符,并且可以识别 python 关键字,对后一个要求,你可以使用 keyword 模块(特别是 keyword.kelist)来帮你.
我最初的代码是:
代码如下:
#!/usr/bin/env python
import string
import keyword
import sys
#get all keyword for python
#keyword.kwlist
#[‘and’, ‘as’, ‘assert’, ‘break’, …]
keywords = keyword.kwlist
#get all character for identifier
#string.letters ==> ‘abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz’
#string.digits ==> ‘0123456789’
charforid = string.letters + “_”
numforid = string.digits
idinput = raw_input(“input your words,please!”)
if idinput in keywords:
print “%s is keyword fot python!” % idinput
else:
lennum = len(idinput)
if(1 == lennum):
if(idinput in charforid and idinput != “_”):
print “%s is legal identifier for python!” % idinput
else:
#it’s just “_”
print “%s isn’t legal identifier for python!” % idinput
else:
if(idinput[0:1] in charforid):
legalstring = charforid + numforid
for item in idinput[1:]:
if (item not in legalstring):
print “%s isn’t legal identifier for python!” % idinput
sys.exit(0)
print “%s is legal identifier for python!2” % idinput
else:
print “%s isn’t legal identifier for python!3” % idinput
代码完毕后,我测试每一条分支,测试到分支时,必须输入_d4%等包含非法字符的标识符才能进行测试,我最初以为,sys.exit(0)—正常退出脚本,sys.exit(1)非正常退出脚本,但是实际情况是/9sys.exit(1),仅输出返回码不同):
代码如下:
if (item not in legalstring):
print “%s isn’t legal identifier for python!” % idinput
sys.exit(0)
input your words,please!_d4%
_d4% isn’t legal identifier for python!
traceback (most recent call last):
file “e:/python/idcheck.py”, line 37, in
sys.exit(0)
systemexit: 0
>>>
由此可见,这样做没有达到我预期如下输出的效果,那么,问题在哪里呢?在于sys.exit()始终会抛出一个systemexit异常。
代码如下:
input your words,please!_d4%
_d4% isn’t legal identifier for python!
代码如下:
#!/usr/bin/env python
import string
import keyword
import sys
import traceback
try:
#get all keyword for python
#keyword.kwlist
#[‘and’, ‘as’, ‘assert’, ‘break’, …]
keywords = keyword.kwlist
#get all character for identifier
#string.letters ==> ‘abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz’
#string.digits ==> ‘0123456789’
charforid = string.letters + “_”
numforid = string.digits
idinput = raw_input(“input your words,please!”)
if idinput in keywords:
print “%s is keyword fot python!” % idinput
else:
lennum = len(idinput)
if(1 == lennum):
if(idinput in charforid and idinput != “_”):
print “%s is legal identifier for python!” % idinput
else:
#it’s just “_”
print “%s isn’t legal identifier for python!” % idinput
else:
if(idinput[0:1] in charforid):
legalstring = charforid + numforid
for item in idinput[1:]:
if (item not in legalstring):
print “%s isn’t legal identifier for python!” % idinput
sys.exit()
print “%s is legal identifier for python!2” % idinput
else:
print “%s isn’t legal identifier for python!3” % idinput
except systemexit:
pass
except:
traceback.print_exc()
上面的代码获取sys.exit()抛出的systemexit异常。
return:在定义函数时从函数中返回一个函数的返回值,终止函数的执行。
exit:下面的代码中,如果把sys.exit()替换成exit,则exit仅仅跳出离它最近的for循环, print “%s is legal identifier for python!2” % idinput语句会被输出,这里,exit的作用类似于break. 但实际上break和exit作用并不同
代码如下:
for item in idinput[1:]:
if (item not in legalstring):
print “%s isn’t legal identifier for python!” % idinput
sys.exit()
print “%s is legal identifier for python!2” % idinput