有這樣一道題目: 字符串標(biāo)識(shí)符.修改例 6-1 的 idcheck.py 腳本,使之可以檢測(cè)長(zhǎng)度為一的標(biāo)識(shí)符,并且可以識(shí)別 Python 關(guān)鍵字,對(duì)后一個(gè)要求,你可以使用 keyword 模塊(特別是 keyword.kelist)來(lái)幫你.
我最初的代碼是:
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
代碼完畢后,我測(cè)試每一條分支,測(cè)試到分支時(shí),必須輸入_d4%等包含非法字符的標(biāo)識(shí)符才能進(jìn)行測(cè)試,我最初以為,sys.exit(0)---正常退出腳本,sys.exit(1)非正常退出腳本,但是實(shí)際情況是/9sys.exit(1),僅輸出返回碼不同):
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 <module>
sys.exit(0)
SystemExit: 0
>>>
由此可見(jiàn),這樣做沒(méi)有達(dá)到我預(yù)期如下輸出的效果,那么,問(wèn)題在哪里呢?在于sys.exit()始終會(huì)拋出一個(gè)SystemExit異常。
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:在定義函數(shù)時(shí)從函數(shù)中返回一個(gè)函數(shù)的返回值,終止函數(shù)的執(zhí)行。
exit:下面的代碼中,如果把sys.exit()替換成exit,則exit僅僅跳出離它最近的for循環(huán), print "%s is legal identifier for Python!2" % idInput語(yǔ)句會(huì)被輸出,這里,exit的作用類(lèi)似于break. 但實(shí)際上break和exit作用并不同
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注