当前位置:网站首页>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()
边栏推荐
- 杰理之MUSIC 模式获取播放文件的目录【篇】
- [redis]配置文件
- 数据库总结:mysql在开发过程中常见的问题及优化
- Jerry's problem of opening the near end of four channel call [chapter]
- PlainSelect. getGroupBy()Lnet/sf/jsqlparser/statement/select/GroupByElement;
- 牛客 52次月赛 C 说谎的机器 (区间赋值操作由O(n^2)转为O(n)的复杂度)
- 94-SQL优化案例一则(用到的写法经常是被嫌弃的)
- ICML2022 | 利用虚拟节点促进图结构学习
- 513. find the value in the lower left corner of the tree / Sword finger offer II 091 Paint the house
- 《跟唐老师学习云网络》 - OpenStack网络实现
猜你喜欢
![DACL output on Jerry's hardware, DAC output sound of left and right channels [chapter]](/img/8a/ce164a5538bd8edf10eba5e4e8abe6.png)
DACL output on Jerry's hardware, DAC output sound of left and right channels [chapter]

How swiftui simulates the animation effect of view illumination increase

第019讲:函数:我的地盘听我的 | 课后测试题及答案

2022 group programming TIANTI race L1

杰理之AUX 模式使用 AUX1或者 AUX2通道时,程序会复位问题【篇】

杰理之列免晶振一拖八烧录升级【篇】

【ICML2022】利用虚拟节点促进图结构学习

CVPR2022 | 海德堡大学《深度视觉相似性与度量学习》教程

Fluent system architecture

Adblock屏蔽百度热搜
随机推荐
(DUC/DDC)数字上混频/正交下混频原理及matlab仿真
Apple corefoundation source code
Objective-C byte size occupied by different data types
Baijia forum in the 13th year of Yongzheng (lower part)
Japanese anime writers and some of their works
什么是数据资产?数据资产管理应该如何落地?
An example of 89 Oracle SQL writing and optimizer defects
第027讲:集合:在我的世界里,你就是唯一 | 课后测试题及答案
Redis的使用场景分享(项目实战)
513. find the value in the lower left corner of the tree / Sword finger offer II 091 Paint the house
Jerry's near end tone change problem of opening four channel call [chapter]
[redis]redis6 transaction operations
Install MySQL in ECS (version 2022)
[redis]Redis6的主从复制
[records of different objects required by QIPA]
鸿蒙第三次培训
基于C语言开发工资管理系统 课程论文+源码及可执行exe文件
Introduce sparse activation mechanism! Uni perceiver MOE significantly improves the performance of generalist model
[redis]Redis6的事务操作
Lesson 020: functions: embedded functions and closures | after class test questions and answers