Python第三章 異常處理

2021-10-08 12:03:22 字數 4027 閱讀 4605

章節練習

try:

檢測範圍

except exception[as reason]:

出現異常後的處理**

try

: f =

open

('test.txt'

)print

(f.read())

f.close(

)except oserror:

print

('開啟檔案出錯'

)# 開啟檔案出錯--

----

----

----

----

----

----

----

--try:

f =open

('test.txt'

)print

(f.read())

f.close(

)except oserror as error:

print

('開啟檔案出錯\n原因是:'

+str

(error)

)# 開啟檔案出錯

# 原因是:[errno 2] no such file or directory: 'test.txt'--

----

----

----

----

----

----

----

--//可以多個異常一起捕獲,但是乙個時間只能有乙個except被執行

try:

int(

"abc"

) s =1+

'1' f =

open

('test.txt'

)print

(f.read())

f.close(

)except oserror as error:

print

('開啟檔案出錯\n原因是:'

+str

(error)

)except typeerror as error:

print

('型別出錯\n原因是:'

+str

(error)

)except valueerror as error:

print

('數值出錯\n原因是:'

+str

(error)

)# 數值出錯

# 原因是:invalid literal for int() with base 10: 'abc'--

----

----

----

----

----

----

----

--//在乙個except 後寫多個異常,用括號連線起來 as error ;

try:

s =1+

'1'int

("abc"

) f =

open

('test.txt'

)print

(f.read())

f.close(

)except

(oserror, typeerror, valueerror)

as error:

print

('出錯了!\n原因是:'

+str

(error)

)# 出錯了!

# 原因是:unsupported operand type(s) for +: 'int' and 'str'

dict1 =

try:

x = dict1[

'y']

except keyerror:

print

('鍵錯誤'

)except lookuperror:

print

('查詢錯誤'

)else

:print

(x)# 鍵錯誤

lookuperror中包含keyerror異常,父子關係,有順序要求,如果父類在前面的話,那麼後面的子類永遠不會執行。

注意:else語句的存在必須以except語句的存在為前提,在沒有except語句的try語句中使用else語句,會引發語法錯誤。

try

: 檢測範圍

except exception[

as reason]

: 出現異常後的處理**

finally

: 無論如何都會被執行的**

try

: result = x / y

print

("result is"

, result)

except zerodivisionerror:

print

("division by zero!"

)finally

:print

("executing finally clause"

)divide(2,

1)# result is 2.0

# executing finally clause

divide(2,

0)# division by zero!

# executing finally clause

divide(

"2",

"1")

# executing finally clause

# typeerror: unsupported operand type(s) for /: 'str' and 'str'

try

:raise nameerror(

'hithere'

)except nameerror:

print

('an exception flew by!'

)

1、猜數字遊戲

題目描述:

電腦產生乙個零到100之間的隨機數字,然後讓使用者來猜,如果使用者猜的數字比這個數字大,提示太大,否則提示太小,當使用者正好猜中電腦會提示,「恭喜你猜到了這個數是…」。在使用者每次猜測之前程式會輸出使用者是第幾次猜測,如果使用者輸入的根本不是乙個數字,程式會告訴使用者"輸入無效"。

(嘗試使用try catch異常處理結構對輸入情況進行處理)

獲取隨機數採用random模組。

import random

print

('請輸入0到100之間的整數.'

)number=random.randint(0,

100)

# 生成1到100之間的乙個隨機整數

#guess=int(input("第1次輸猜,請輸入乙個整形的數字 : "))

print

(f"隨機的數字是")n=

1while

true

:try

: guess =

int(

input

(f"第次輸猜,請輸入乙個整形的數字 : "))

except

:print

('輸入無效'

)# 如果int轉化失敗就說明不是整數

else

:if guess < number:

print

('太小'

)elif guess > number:

print

('太大'

)else

:print

(f'恭喜你猜到了這個數是'

)print

("game over!"

)break

n +=

1

Python第三章總結

今天看到了第三章,第三章主要介紹了列表,我認為列表就是之前學習的陣列,內容很簡單,就是有幾個函式容易弄混。這個是乙個列表的例子 bicycles trek cannondale redline specialized 想要獲取某乙個元素時,比如第乙個元素,可以使用 bicycles python為訪...

python 自然語言處理第三章

1.訪問檔案 a.本地檔案 import os file open path 指標 file.read 得到字串 for line in file 遍歷檔案的每一行 b.網路檔案 from urllib import urlopen file urlopen url file.read 2.分詞 t...

第三章 堆疊

1.基礎知識 堆疊可以實現很多的應用,遞迴的問題轉化成非遞迴形式,在本質上也是堆疊的問題.它是一種 filo 操作的資料結構,一般也有兩種儲存方式 陣列跟鍊錶實現形式,這裡我給出了鍊錶形式的堆疊模板,裡面包括了基本的堆疊所有的操作,還有兩個比較著名的應用例子,時間倉促,精力比較有限,關於迷宮老鼠還沒...