一段Python代码,前面都看得懂,就是最后四行看不懂诶,烦请解答! 20
烦请详解最后四行,谢谢:#-*-coding:utf-8-*-importstringimportkeywordimportsysimporttracebacktry:k...
烦请详解最后四行,谢谢:
# -*- coding: utf-8 -*-
import string
import keyword
import sys
import traceback
try:
keyWords=keyword.kwlist
charForId=string.letters+'_'
numForId=string.digits
idInput=raw_input("请输入您的关键字:")
if idInput in keyWords:
print "%s 是Python的关键字!"% idInput
else:
lenNum=len(idInput)
if(1==lenNum):
if(idInput in charForId and idInput !="_"):
print "%s 是合法的Python标示符!"% idInput
else:
print "%s 不是合法的Python标示符!"% idInput
else:
if (idInput[0:1] in charForId):
legalstring=charForId+numForId
for item in idInput[1:]:
if (item not in legalstring):
print "%s 不是合法的Python标示符!"% idInput
sys.exit()
print "%s 是合法的Python标示符!2"% idInput
else:
print "%s 不是合法的Python标示符!3"% idInput
except SystemExit:
pass
except:
traceback.print_exc() 展开
# -*- coding: utf-8 -*-
import string
import keyword
import sys
import traceback
try:
keyWords=keyword.kwlist
charForId=string.letters+'_'
numForId=string.digits
idInput=raw_input("请输入您的关键字:")
if idInput in keyWords:
print "%s 是Python的关键字!"% idInput
else:
lenNum=len(idInput)
if(1==lenNum):
if(idInput in charForId and idInput !="_"):
print "%s 是合法的Python标示符!"% idInput
else:
print "%s 不是合法的Python标示符!"% idInput
else:
if (idInput[0:1] in charForId):
legalstring=charForId+numForId
for item in idInput[1:]:
if (item not in legalstring):
print "%s 不是合法的Python标示符!"% idInput
sys.exit()
print "%s 是合法的Python标示符!2"% idInput
else:
print "%s 不是合法的Python标示符!3"% idInput
except SystemExit:
pass
except:
traceback.print_exc() 展开
展开全部
try..except是一个错误处理语法
Python 错误处理
http://www.codemud.net/~thinker/GinGin_CGI.py/show_id_doc/302
python学习笔记——异常
http://www.cnblogs.com/rubylouvre/archive/2011/06/22/2086644.html
Python 错误处理
http://www.codemud.net/~thinker/GinGin_CGI.py/show_id_doc/302
python学习笔记——异常
http://www.cnblogs.com/rubylouvre/archive/2011/06/22/2086644.html
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询