python中exit的作用_Python中exit、return、sys.exit()等使用实例和区别

有这样一道题目: 字符串标识符.修改例 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
from PySide2.QtCore import * from PySide2.QtWidgets import * from PySide2.QtWebEngineWidgets import * from selenium import webdriver from selenium.webdriver.chrome.service import Service from selenium.webdriver.chrome.options import Options class TabWidget(QTabWidget): def init(self, *args, **kwargs): QTabWidget.init(self, *args, **kwargs) self.setup_browser() self.load_pages() def setup_browser(self): options = Options() options.add_argument('--ignore-certificate-errors') options.add_experimental_option('excludeSwitches', ['enable-automation']) options.add_argument("--disable-blink-features=AutomationControlled") options.add_argument('--disable-gpu') options.add_argument('blink-settings=imagesEnabled=false') options.binary_location = './chrome-win/chrome.exe' driver_path = Service("chromedriver.exe") self.driver = webdriver.Chrome(service=driver_path, options=options) def load_pages(self): self.load_page("https://www.163.com", "网易新闻") def load_page(self, url, title): view = HtmlView(self) view.load(QUrl(url)) ix = self.addTab(view, title) self.setCurrentIndex(ix) class HtmlView(QWebEngineView): def init(self, *args, **kwargs): QWebEngineView.init(self, *args, **kwargs) self.tab = self.parent() def createWindow(self, windowType): if windowType == QWebEnginePage.WebBrowserTab: webView = HtmlView(self.tab) ix = self.tab.addTab(webView, "加载 ...") self.tab.setCurrentIndex(ix) return webView return QWebEngineView.createWindow(self, windowType) if name == "main": import sys app = QApplication(sys.argv) main = TabWidget() main.show() sys.exit(app.exec_()),请优化这段代码
最新发布
06-08

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值