日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > python >内容正文

python

python 示例_Python中带有示例的关键字除外

發(fā)布時(shí)間:2023/12/1 python 61 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python 示例_Python中带有示例的关键字除外 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

python 示例

Python關(guān)鍵字除外 (Python except keyword)

except is a keyword (case-sensitive) in python, it is used with try... except statement to handle the exception.

除了是python中的一個(gè)關(guān)鍵字(區(qū)分大小寫),它與try ... except語(yǔ)句一起使用來(lái)處理異常。

except keyword defines a block which executes if statements are written in try block raise an error.

else關(guān)鍵字定義了一個(gè)塊,如果在try塊中編寫了語(yǔ)句,該塊將執(zhí)行,并引發(fā)錯(cuò)誤。

Note: We can define multiple blocks with except keyword to handle the different types of expectations by mentioning the error/exception names.

注意:我們可以通過(guò)提及錯(cuò)誤/異常名稱,使用except關(guān)鍵字定義多個(gè)塊以處理不同類型的期望。

Syntax of except keyword

關(guān)鍵字除外的語(yǔ)法

try:statement(s)-1except:statement(s)-2

While executing the statement(s)-1, if there is any exception raises, control jumps to except block and statement(s)-2 executes.

在執(zhí)行語(yǔ)句-1時(shí) ,如果引發(fā)任何異常,則控制跳轉(zhuǎn)到塊和語(yǔ)句2 除外 。

Syntax of except keyword with multiple except blocks

具有多個(gè)except塊的except關(guān)鍵字的語(yǔ)法

try:statement(s)-1except Error_Name1:statement(s)-Aexcept Error_Name2:statement(s)-Bexcept Error_Name3:statement(s)-C..except:statement(s)-default

While executing the statement(s)-1 if Error_Name1 generates, statements written in Except Error_Name1 (statements(s)-A) executes, and so on... If any error is not mentioned with except block then last except block without any error name executes.

如果Error_Name1生成,則在執(zhí)行語(yǔ)句-1時(shí),將執(zhí)行用Error_Name1 以外的語(yǔ)句編寫的語(yǔ)句( 語(yǔ)句-A ),依此類推...如果未提及任何錯(cuò)誤,則除了block 以外,最后一個(gè)exception 都沒(méi)有錯(cuò)誤名稱執(zhí)行。

Example:

例:

Input:a = 10b = 0try:# no errorresult = a%bprint(result)except:print("There is an error")Output:There is an error

Python的除外關(guān)鍵字示例 (Python examples of except keyword)

Example 1: Find modulus of two number and handle exception, if divisor is 0.

示例1:如果除數(shù)為0,則求兩個(gè)數(shù)的模數(shù)并處理異常。

# python code to demonstrate example of # except keyword # Find modulus of two number and # handle exception, if divisor is 0a = 10 b = 3try:# no errorresult = a%bprint(result)# assign 0 to b# an error will occurb = 0result = a%bprint(result)except:print("There is an error")

Output

輸出量

1 There is an error

Example 2: Write an example to handle multiple errors.

示例2:編寫示例以處理多個(gè)錯(cuò)誤。

# python code to demonstrate example of # except keyword # Write an example to handle multiple errorsa = 10 b = 3try:# del b # uncomment this to test NameError# no errorresult = a%bprint(result)# assign 0 to b# an error will occurb = 0result = a%bprint(result)except ZeroDivisionError:print("Can not divide by 0") except NameError:print("A NameError in the code") except:print("There is an error")

Output

輸出量

1 Can not divide by 0

翻譯自: https://www.includehelp.com/python/except-keyword-with-example.aspx

python 示例

總結(jié)

以上是生活随笔為你收集整理的python 示例_Python中带有示例的关键字除外的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。