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

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

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

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

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

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

[[352683]]

現在,我們定義一個函數“summation”,將兩個數字相加。該函數運行正常。

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

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

  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”錯誤出現了,因為我們試圖將數字和字符串相加。請注意,錯誤出現后,后面的代碼便不再執行。所以我們要用到上面提到的關鍵詞,確保即使出錯,腳本依舊運行。

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

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

  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塊我們還標明了錯誤類型。如果沒有標明錯誤類型,出現一切異常都會執行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 

 

現在,故意創造一個錯誤,看看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

編程高并發

2011-06-20 14:32:59

關鍵詞

2015-07-27 11:11:31

混合云混合云管理云服務

2016-11-01 14:33:18

郝聯峰大數據

2020-03-25 15:07:21

數據挖掘大數據思維

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緩存數據庫

2021-12-23 10:05:43

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

2011-06-16 17:54:25

關鍵詞

2019-11-08 14:55:45

區塊鏈密碼量子計算

2011-06-21 16:11:04

SEO關鍵詞

2011-05-25 17:58:00

2013-08-26 15:43:40

AppStore關鍵詞開發者應用選取關鍵詞

2019-12-22 13:48:26

退休科技行業大佬
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 日韩av高清 | 综合色在线 | 欧美一级全黄 | 久久久久国产精品一区三寸 | 久久精品99久久 | 午夜欧美a级理论片915影院 | 在线免费观看黄视频 | 国产午夜亚洲精品不卡 | 午夜免费在线观看 | 黄色网址免费在线观看 | 亚洲精品电影网在线观看 | h在线| 欧美精品区 | 欧美国产一区二区 | 男人的天堂亚洲 | 久草99| 国产乱码精品一品二品 | 一区二区三区视频在线免费观看 | 欧美一区2区三区4区公司 | 日韩欧美三级 | 91精品久久久久久久久 | 国产99久久精品一区二区永久免费 | 精品久久久久久亚洲综合网 | 亚洲成av片人久久久 | 国精产品一区二区三区 | 一区二区在线 | 在线亚洲电影 | 国户精品久久久久久久久久久不卡 | 日韩欧美视频免费在线观看 | 免费特级黄毛片 | 久久久久久久久久久91 | 看片wwwwwwwwwww | 自拍视频一区二区三区 | 国产区视频在线观看 | 人人精品 | 99精品免费 | 亚洲欧美在线免费观看 | 欧美在线视频一区 | 99精品电影| 一区二区三区视频在线观看 | 国产羞羞视频在线观看 |