当前位置:网站首页>Lesson 032: exception handling: you can't always be right | after class test questions and answers
Lesson 032: exception handling: you can't always be right | after class test questions and answers
2022-06-22 21:36:00 【ChaseTimLee】
Test questions :
0. Combine your own programming experience , Summarize the importance of exception handling mechanisms ?
[Python Summary of standard exceptions ] link
answer : Due to the uncertainty of the environment and the unpredictability of user operations, various problems may occur in the program , So the most important exception mechanism is nothing more than : Enhance program robustness and user experience , Try to catch all the predicted exceptions and write the code to handle them , When an exception occurs , The program automatically digests and returns to normal ( Not to collapse ).|
1. Will the following code generate an exception , If so , Please write the name of the exception :
>>> my_list = [1, 2, 3, 4,,]
answer : Grammar mistakes
SyntaxError: invalid syntax
2. Will the following code generate an exception , If so , Please write the name of the exception :
>>> my_list = [1, 2, 3, 4, 5]
>>> print(my_list[len(my_list)])
answer :len(my_list) Is the length of the list , The length here is 5, The access index numbers of each element of the list are :[0, 1, 2, 3, 4], So try to access my_list(5) May trigger IndexError: list index out of range abnormal .
3. Will the following code generate an exception , If so , Please write the name of the exception :
>>> my_list = [3, 5, 1, 4, 2]
>>> my_list.sorted()
answer : Mistakes that beginners tend to overlook , The sorting method of the list is called list.sort(),sorted() yes BIF. So it will trigger AttributeError: ‘list’ object has no attribute ‘sorted’ abnormal .
4. Will the following code generate an exception , If so , Please write the name of the exception :
>>> my_dict = {
'host': 'http://bbs.fishc.com', 'port': '80'}
>>> print(my_dict['server'])
answer : Try to access a nonexistent in the dictionary “ key ” trigger KeyError: ‘server’ abnormal , To avoid this exception , have access to dict.get() Method
5. Will the following code generate an exception , If so , Please write the name of the exception :,=R)k!C
def my_fun(x, y):
print(x, y)
my_fun(x=1, 2)
answer : If you use keyword parameters , Both parameters need to use the keyword parameter my_fun(x=1, y=2)
6. Will the following code generate an exception , If so , Please write the name of the exception :
f = open('C:\\test.txt', wb)
f.write('I love FishC.com!\n')
f.close()
answer : Be careful open() The second parameter is the string , should f = open(‘C:\test.txt’, ‘wb’) .wb No double quotes Python I thought it was a variable name , Look up , Emma couldn't find it …… trigger NameError abnormal . Failed to open file , Then a series of and f All relevant will report the same exception .
7. Will the following code generate an exception , If so , Please write the name of the exception :
def my_fun1():
x = 5
def my_fun2():
x *= x
return x
return my_fun2()
my_fun1()
answer : Do you still remember the knowledge of closures ? Python We think that in the internal function x When it's a local variable , Of external functions x It's blocked , So execute x *= x When , You can't find a local variable on the right x Value , Therefore, an error was reported .
stay Python3 There was no direct solution before , Can only be stored indirectly by container type , Because the container type is not on the stack , So it won't be “ shielding ” fall . Is the word container type familiar to everyone ? The string we introduced earlier 、 list 、 Yuan Zu , What can be thrown in is the type of container .
def my_fun1():
x = [5]
def my_fun2():
x[0] *= x[0]
return x[0]
return my_fun2()
my_fun1()
But here we are Python3 In the world of , There have been many improvements , If we want to modify the value of the local variable in the external function in the internal function , Then there is also a keyword that can be used , Namely nonlocal:
def my_fun1():
x = 5
def my_fun2():
nonlocal x
x *= x
return x
return my_fun2()
my_fun1()
边栏推荐
- ByteDance proposes a lightweight and efficient new network mocovit, which has better performance than GhostNet and mobilenetv3 in CV tasks such as classification and detection
- 杰理之开启四声道通话近端卡顿问题【篇】
- 2022年A特种设备相关管理(电梯)考题及模拟考试
- [redis]redis的持久化操作
- redis学习笔记
- 第028讲:文件:因为懂你,所以永恒 | 课后测试题及答案【无标题】
- 第026讲:字典:当索引不好用时2 | 课后测试题及答案
- kali2021安装RTL8188GU无线网卡[TL-WN726N]驱动
- 70 root cause analysis Oracle database sudden performance problems, who will take the blame
- 第023、024讲:递归:这帮小兔崽子、汉诺塔 | 课后测试题及答案
猜你喜欢

Redis learning notes

300. longest increasing subsequence ●●

Lesson 018: function: flexible is powerful after class test questions and answers
![[20. valid brackets]](/img/e9/50f327048055b07b68e694a9003130.png)
[20. valid brackets]

2022 chemical automation control instrument examination exercises and online simulation examination

2022危险化学品经营单位主要负责人上岗证题库及模拟考试
![[redis]三种新数据类型](/img/ce/8a048bd36b21bfa562143dd2e47131.png)
[redis]三种新数据类型

TC397 Flash
![[redis]发布与订阅](/img/50/0c2fbbb8f56fccdd3222b77efdd723.png)
[redis]发布与订阅
![[160. cross linked list]](/img/79/177e2c86bd80d12f42b3edfa1974ec.png)
[160. cross linked list]
随机推荐
Android kotlin SP DP to PX
什么是数据资产?数据资产管理应该如何落地?
92 match for several_ Recognize SQL write example
[redis] profile
5分钟快速上线Web应用和API(Vercel)
2022 a special equipment related management (elevator) examination questions and simulation examination
基于C语言开发工资管理系统 课程论文+源码及可执行exe文件
[513. find the value in the lower left corner of the tree]
PHP image making
第019讲:函数:我的地盘听我的 | 课后测试题及答案
How to operate redis on the IntelliJ idea database console
Lesson 020: functions: embedded functions and closures | after class test questions and answers
杰理之开启四声道通话近端变调问题【篇】
Apple corefoundation source code
513. 找树左下角的值 / 剑指 Offer II 091. 粉刷房子
300. longest increasing subsequence ●●
2022 question bank and simulated examination for work license of main principals of hazardous chemical business units
[redis] cluster and common errors
不同网络结构的特征也能进行对比学习?蚂蚁&美团&南大&阿里提出跨架构自监督视频表示学习方法CACL,性能SOTA!
引入稀疏激活机制!Uni-Perceiver-MoE显著提升通才模型的性能