当前位置:网站首页>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()


原网站

版权声明
本文为[ChaseTimLee]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206222005193783.html