This repository was archived by the owner on Jun 20, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathListIndices.py
More file actions
37 lines (34 loc) · 1.31 KB
/
Copy pathListIndices.py
File metadata and controls
37 lines (34 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
a_list = [1, 2, 3, 4, 5]
list_index = 7
#-----------------------------------------------------------
#You may modify the lines of code above, but don't move them!
#When you Submit your code, we'll change these lines to
#assign different values to the variables.
#In this problem, we're going to use some unfamiliar syntax.
#You'll learn more about this syntax in Unit 4. For now,
#though, you don't need to understand the syntax. All you
#need to know is that right now, this code will cause an
#error with the values supplied above.
#
#Revise this code so that this error is caught, and the
#message "Invalid index!!" is printed. However, your revision
#must meet these requirements, too:
#
# - If the values of the variables above are changed so
# that the error doesn't occur, the program should behave
# the same way that it does now.
# - The two first and last lines, "Accessing index..." and
# "Done!", should print whether or not an error occurs.
# - If a *different* error occurs from the one that arises
# initially, your code should instead print "Unknown
# error!"
# Access data from a_list safely
print("Accessing index...")
try:
result = a_list[list_index]
print("Value at index:", result)
except IndexError:
print("Invalid index!!")
except Exception as e:
print("Unknown error!")
print("Done!")