日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
Python報(bào)錯(cuò)不要慌,這三個(gè)關(guān)鍵詞幫你解決問題!

本文轉(zhuǎn)載自公眾號(hào)“讀芯術(shù)”(ID:AI_Discovery)。

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

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

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

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

接下來(lái),我們讓用戶輸入其中一個(gè)數(shù)字,并運(yùn)行該函數(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.  in  
  7. ----> 1 summation(num1,num2) 
  8.       2 print("This line will notbe printed because of the error") 
  9.  
  10.  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”錯(cuò)誤出現(xiàn)了,因?yàn)槲覀冊(cè)噲D將數(shù)字和字符串相加。請(qǐng)注意,錯(cuò)誤出現(xiàn)后,后面的代碼便不再執(zhí)行。所以我們要用到上面提到的關(guān)鍵詞,確保即使出錯(cuò),腳本依舊運(yùn)行。

 
 
 
 
  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)錯(cuò)誤,except塊的代碼開始運(yùn)行,并打印語(yǔ)句。接下來(lái)加入“else”塊,來(lái)應(yīng)對(duì)沒有錯(cuò)誤出現(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 

接下來(lái)我們用另外一個(gè)例子理解。這個(gè)例子中,在except塊我們還標(biāo)明了錯(cuò)誤類型。如果沒有標(biāo)明錯(cuò)誤類型,出現(xiàn)一切異常都會(huì)執(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)造一個(gè)錯(cuò)誤,看看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 

網(wǎng)站名稱:Python報(bào)錯(cuò)不要慌,這三個(gè)關(guān)鍵詞幫你解決問題!
文章URL:http://www.5511xx.com/article/cdieeih.html