自学python第八周总结

异常

处理ZeroDivisiongError异常:
一些简单错误的操作容易引起python引发异常而不发继续下去。
如下所示:

>>> print (5/0)
Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    print (5/0)
ZeroDivisionError: division by zero

python会指出错误,并且指出错误的地方。
解决方法如下:
1:使用 try-except 代码块
如下所示:

try:
    print(5/0)

except ZeroDivisionError:
    print('error,zero is not allow.')

error,zero is not allow.
如果try代码块中的代码没有问题的话,python将会跳过except代码块;如果try中的代码有问题的话,python将会查找这样的except代码块,并运行其中的代码。

使用异常避免崩溃:妥善处理错误,为了不让程序崩溃。

print('please input two numbers,i will divide them.')
print('enter "q" to quit.')

while True:
    first_number = input('\nfirtt_number:')
    if first_number == 'q':
        break

    second_number = input('\nsecond_number:')

    if second_number == 'q':
        break

    answer = int(first_number) / int(second_number)
    print(answer)
please input two numbers,i will divide them.
enter "q" to quit.

firtt_number:12

second_number:2
6.0

firtt_number:12

second_number:0
Traceback (most recent call last):
  File "H:/python/fishc/divisionn.py", line 14, in <module>
    answer = int(first_number) / int(second_number)
ZeroDivisionError: division by zero

else代码块:依赖于try代码块成功执行的代码都应放到else代码块中去,如下所示:

print('please input two numbers,i will divide them.')
print('enter "q" to quit.')

while True:
    first_number = input('\nfirst_number:')
    if first_number == 'q':
        break

    second_number = input('\nsecond_number:')

    try:
        answer = int(first_number) / int(second_number)

    except ZeroDivisionError:
        print('you can not divide by zero.')

    else:
        print(answer)
please input two numbers,i will divide them.
enter "q" to quit.

first_number:15

second_number:5
3.0

first_number:17

second_number:7
2.4285714285714284

first_number:10

second_number:0
you can not divide by zero.

first_number:15

second_number:3
5.0

**处理FileNotFoundError异常:**对于一些找文件路径错误、无该文件的情况下,也可以使用try-except代码块。

file_name = 'alice.txt'

with open(file_name) as fishc:
    contents = fishc.read()

输出结果如下:

Traceback (most recent call last):
  File "H:/python/fishc/divisionn.py", line 3, in <module>
    with open(file_name) as fishc:
FileNotFoundError: [Errno 2] No such file or directory: 'alice.txt'

用try-except代码块如下所示:

file_name = 'alice.txt'

try:
    with open(file_name) as fishc:
        contents = fishc.read()

except FileNotFoundError:
    msg = 'sorry, there is not ' + file_name + 'which do you want to open.'
    print(msg)
sorry, there is not alice.txtwhich do you want to open.

分析文本:提取文件中包含多少个单词,也就是字符串,使用split(),只能提取字符串的个数:

>>> title = 'alice in wonderlang'
>>> title.split()
['alice', 'in', 'wonderlang']

split函数以空格为分隔符将字符串识别出来。

file_name = 'jade.txt'

try:
    with open(file_name) as fishc:
        contents = fishc.read()

except FileNotFoundError:
    msg = 'sorry, there is not ' + file_name + 'which do you want to open.'
    print(msg)

else:
    words = contents.split()
    num_words = len(words)
    print('the file ' + file_name + 'has about ' +str(num_words) + 'words')

the file jade.txthas about 5words

使用多个文件:与上述代码基本一致,差别只是多添加一些文本,并且加一个for循环语句即可。

file_names = ['allen.txt' , 'kobe.txt' , 'jay.txt']
for file_name in file_names:
    count_words(file_name)

失败时一声不吭:令一些长程序在发生异常时一声不吭,不告诉用户。也是在try-except代码块下进行的一种操作,可以使用pass函数,可以在代码块中使用它来让python什么都不要做:

file_name = 'jade.txt'

try:
    with open(file_name) as fishc:
        contents = fishc.read()

except FileNotFoundError:
    pass

else:
    words = contents.split()
    num_words = len(words)
    print('the file ' + file_name + 'has about ' +str(num_words) + 'words')
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值