Python中exit、return、sys.exit()等使用實(shí)例和區(qū)別
有這樣一道題目: 字符串標(biāo)識符.修改例 6-1 的 idcheck.py 腳本,使之可以檢測長度為一的標(biāo)識符,并且可以識別 Python 關(guān)鍵字,對后一個(gè)要求,你可以使用 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
代碼完畢后,我測試每一條分支,測試到分支時(shí),必須輸入_d4%等包含非法字符的標(biāo)識符才能進(jìn)行測試,我最初以為,sys.exit(0)---正常退出腳本,sys.exit(1)非正常退出腳本,但是實(shí)際情況是/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 <module>
sys.exit(0)
SystemExit: 0
>>>
由此可見,這樣做沒有達(dá)到我預(yù)期如下輸出的效果,那么,問題在哪里呢?在于sys.exit()始終會拋出一個(gè)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:在定義函數(shù)時(shí)從函數(shù)中返回一個(gè)函數(shù)的返回值,終止函數(shù)的執(zhí)行。
exit:下面的代碼中,如果把sys.exit()替換成exit,則exit僅僅跳出離它最近的for循環(huán), print "%s is legal identifier for Python!2" % idInput語句會被輸出,這里,exit的作用類似于break. 但實(shí)際上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
- python中return不返回值的問題解析
- python中return的返回和執(zhí)行實(shí)例
- python 實(shí)現(xiàn)return返回多個(gè)值
- Python Django view 兩種return的實(shí)現(xiàn)方式
- 對python中return與yield的區(qū)別詳解
- 通過實(shí)例解析Python return運(yùn)行原理
- python return邏輯判斷表達(dá)式實(shí)現(xiàn)解析
- Python中return語句用法實(shí)例分析
- Python return語句如何實(shí)現(xiàn)結(jié)果返回調(diào)用
相關(guān)文章
解讀pandas交叉表與透視表pd.crosstab()和pd.pivot_table()函數(shù)
這篇文章主要介紹了pandas交叉表與透視表pd.crosstab()和pd.pivot_table()函數(shù)的用法,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-09-09詳解python異步編程之a(chǎn)syncio(百萬并發(fā))
這篇文章主要介紹了詳解python異步編程之a(chǎn)syncio(百萬并發(fā)),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-07-07創(chuàng)建SparkSession和sparkSQL的詳細(xì)過程
SparkSession 是 Spark SQL 的入口,Builder 是 SparkSession 的構(gòu)造器。 通過 Builder, 可以添加各種配置,并通過 stop 函數(shù)來停止 SparkSession,本文給大家分享創(chuàng)建SparkSession和sparkSQL的詳細(xì)過程,一起看看吧2021-08-08Python實(shí)現(xiàn)的樸素貝葉斯算法經(jīng)典示例【測試可用】
這篇文章主要介紹了Python實(shí)現(xiàn)的樸素貝葉斯算法,結(jié)合實(shí)例形式詳細(xì)分析了Python實(shí)現(xiàn)與使用樸素貝葉斯算法的具體操作步驟與相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2018-06-06教你Pycharm安裝使用requests第三方庫的詳細(xì)教程
PyCharm安裝第三方庫是十分方便的,無需pip或其他工具,平臺就自帶了這個(gè)功能而且操作十分簡便,今天通過本文帶領(lǐng)大家學(xué)習(xí)Pycharm安裝使用requests第三方庫的詳細(xì)教程,感興趣的朋友一起看看吧2021-07-07