[Python]小甲鱼Python视频第32课(except)课后题及参考解答
# -*- coding: utf-8 -*-
"""
Spyder Editor
This is a temporary script file.
"""
#0. 结合你自身的编程经验,总结下异常处理机制的重要性
#增加程序处理错误的能力,防止程序出现错误就停止运行,提高程序自我纠错能力。
#1
my_list = [1, 2, 3, 4,,]
#语法错误异常: SyntaxError
#2
my_list = [1, 2, 3, 4, 5];
print(my_list[len(my_list)]);
#访问越界异常,索引错误异常 :IndexError
#3
my_list = [3, 5, 1, 4, 2]
my_list.sorted()
#找不到sorted这个方法,属性错误异常 : AttributeError
#4
my_dict = {'host': 'http://bbs.fishc.com', 'port': '80'}
print(my_dict['server'])
#找不到 'server' 这个键,键错误异常:KeyError
#5
def my_fun(x, y):
print(x, y)
my_fun(1, y=2);
#关键字参数应该在位置参数后面, 语法错误, SyntaxError,应该修改为 my_fun(1, y=2);
#6
f = open('C:\\test.txt', wb)
f.write('I love FishC.com!\n')
f.close();
#wb未定义,语法名称错误:NameError
#7
def my_fun1():
x = 5
def my_fun2():
x *= x
return x
return my_fun2()
my_fun1()
# 内层函数中x被改变,但没有定义和初始化 UnboundLocalError
~不再更新,都不让我写公式,博客园太拉胯了

浙公网安备 33010602011771号