成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

Python報錯不要慌,這三個關鍵詞幫你解決問題!

開發(fā) 后端
寫代碼必然會出現(xiàn)錯誤,而錯誤處理可以針對這些錯誤提前做好準備。通常出現(xiàn)錯誤時,腳本會停止運行,而有了錯誤處理,腳本就可以繼續(xù)運行。

本文轉載自公眾號“讀芯術”(ID:AI_Discovery)。

寫代碼必然會出現(xiàn)錯誤,而錯誤處理可以針對這些錯誤提前做好準備。通常出現(xiàn)錯誤時,腳本會停止運行,而有了錯誤處理,腳本就可以繼續(xù)運行。為此,我們需要了解下面三個關鍵詞:

  • try:這是要運行的代碼塊,可能會產(chǎn)生錯誤。
  • except:如果在try塊中出現(xiàn)錯誤,將執(zhí)行這段代碼。
  • finally:不管出現(xiàn)什么錯誤,都要執(zhí)行這段代碼。

[[352683]]

現(xiàn)在,我們定義一個函數(shù)“summation”,將兩個數(shù)字相加。該函數(shù)運行正常。

  1. >>> defsummation(num1,num2): 
  2.         print(num1+num2)>>>summation(2,3) 

接下來,我們讓用戶輸入其中一個數(shù)字,并運行該函數(shù)。

  1. >>> num1 = 2 
  2. >>> num2 = input("Enter number: ") 
  3. Enter number: 3>>> summation(num1,num2)>>> print("Thisline will not be printed because of the error") 
  4. --------------------------------------------------------------------------- 
  5. TypeError                                Traceback (most recent call last) 
  6. <ipython-input-6-2cc0289b921e> in <module> 
  7. ----> 1 summation(num1,num2) 
  8.       2 print("This line will notbe printed because of the error") 
  9.  
  10. <ipython-input-1-970d26ae8592> in summation(num1, num2) 
  11.       1 def summation(num1,num2): 
  12. ----> 2     print(num1+num2) 
  13.  
  14. TypeError: unsupported operand type(s) for +:  int  and  str  

“TypeError”錯誤出現(xiàn)了,因為我們試圖將數(shù)字和字符串相加。請注意,錯誤出現(xiàn)后,后面的代碼便不再執(zhí)行。所以我們要用到上面提到的關鍵詞,確保即使出錯,腳本依舊運行。

  1. >> try: 
  2.         summed = 2 +  3  
  3.     except: 
  4.         print("Summation is not ofthe same type")Summation is not of the same type 

可以看到,try塊出現(xiàn)錯誤,except塊的代碼開始運行,并打印語句。接下來加入“else”塊,來應對沒有錯誤出現(xiàn)的情況。

  1. >>> try: 
  2.         summed = 2 + 3 
  3.     except: 
  4.         print("Summation is not ofthe same type") 
  5.     else: 
  6.         print("There was no errorand result is: ",summed)There was no error and result is:  5 

接下來我們用另外一個例子理解。這個例子中,在except塊我們還標明了錯誤類型。如果沒有標明錯誤類型,出現(xiàn)一切異常都會執(zhí)行except塊。

  1. >>> try: 
  2.         f = open( test , w ) 
  3.         f.write("This is a testfile") 
  4.     except TypeError: 
  5.         print("There is a typeerror") 
  6.     except OSError: 
  7.         print("There is an OSerror") 
  8.     finally: 
  9.         print("This will print evenif no error")This will print even if no error 

 

現(xiàn)在,故意創(chuàng)造一個錯誤,看看except塊是否與finally塊共同工作吧!

  1. >>> try: 
  2.         f = open( test , r ) 
  3.         f.write("This is a testfile") 
  4.     except TypeError: 
  5.         print("There is a typeerror") 
  6.     except OSError: 
  7.         print("There is an OSerror") 
  8.     finally: 
  9.         print("This will print evenif no error")There is an OS error 
  10. This will print even if no error 

 

責任編輯:趙寧寧 來源: 讀芯術
相關推薦

2017-11-02 13:15:18

Linux

2011-06-27 17:07:35

SEO

2011-06-02 18:27:06

關鍵詞標題

2020-10-20 06:45:48

編程高并發(fā)

2011-06-20 14:32:59

關鍵詞

2015-07-27 11:11:31

混合云混合云管理云服務

2016-11-01 14:33:18

郝聯(lián)峰大數(shù)據(jù)

2020-03-25 15:07:21

數(shù)據(jù)挖掘大數(shù)據(jù)思維

2011-06-07 18:45:41

關鍵詞

2018-05-28 14:38:44

PHPPython應用

2015-07-20 10:17:37

云計算應用混合云混合云管理

2011-06-14 19:11:38

關鍵詞

2020-10-09 11:30:07

Redis緩存數(shù)據(jù)庫

2021-12-23 10:05:43

機器學習人工智能黑盒模型

2011-06-16 17:54:25

關鍵詞

2019-11-08 14:55:45

區(qū)塊鏈密碼量子計算

2011-06-21 16:11:04

SEO關鍵詞

2011-05-25 17:58:00

2013-08-26 15:43:40

AppStore關鍵詞開發(fā)者應用選取關鍵詞

2019-12-22 13:48:26

退休科技行業(yè)大佬
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 国产精品色 | 国产亚洲精品美女久久久久久久久久 | 日韩成人免费视频 | 一级毛片黄片 | 欧美激情欧美激情在线五月 | 黄免费观看视频 | 亚洲一区二区精品视频 | 日韩视频在线免费观看 | 国产视频福利一区 | 亚洲一区二区三区在线播放 | 91xx在线观看 | 日韩成人精品 | 成人视屏在线观看 | 成人1区| 国产成人免费一区二区60岁 | 九九热这里只有精品在线观看 | 国产精品久久久久久久久久久免费看 | 亚洲一区二区久久 | 亚洲欧美国产一区二区三区 | 国产成人久久精品一区二区三区 | 久久精品这里精品 | 国产免费一区二区三区 | 亚洲精品久久 | www国产精 | 情侣酒店偷拍一区二区在线播放 | 久久综合爱 | 欧美成年黄网站色视频 | 欧洲高清转码区一二区 | 天天操天天插天天干 | 欧美精品一区在线 | 亚洲精品在线视频 | 国产精品一区二区三区在线 | 九一视频在线观看 | 91在线精品一区二区 | 黑人成人网 | 91丨九色丨国产在线 | 美女在线观看国产 | 欧美一级α片 | 国产精品视频播放 | 男人久久天堂 | 最新国产精品精品视频 |