-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathChapter_8th.py
89 lines (66 loc) · 1.74 KB
/
Chapter_8th.py
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# Example
# 3/0 # type it directly in terminal - it will give you following error
# Traceback (most recent call last):
# File "<ipython-input-31-f6cc6d14333b>", line 1, in <module>
# 3/0
# ZeroDivisionError: division by zero
# Example
divisor = 0
try:
value = 22/divisor
except ZeroDivisionError:
print('The divisor provided was zero.')
# Example
my_numbers=[1,2,3,4]
print('The length of the list is:', len(my_numbers))
#my_numbers[4] # this will give you an error
# Output:
# The length of this list is: 4
# IndexError: list index out of range
# Example
try:
my_numbers[4]
except IndexError:
print('The index you used is invalid.')
print('The code executed.')
# Output:
# The index you used is invalid.
# The code executed.
# Example
def check_type(year):
assert type(year) == int, 'The type of year is not integer'
print('The type of year is valid.')
return True
check_type(year=2010) # The type of year is valid.
# Example this will give you error after uncomment
# check_type(year=2010.22) # AssertionError: The type of year is not integer.
# Example
def check_type(year):
try:
assert type(year) == int
except AssertionError:
print('The type of year is invalid')
return False
return True
print(check_type(year=2010.22))
# The type of year is invalid.
# False
print(check_type(year=2010))
# True
# Example
try:
print(x)
except:
print("An exception occurred")
# Output
# An exception occurred
# Example
try:
print(x)
except:
print("Something went wrong")
finally:
print("The 'try except' is finished")
# Output
# Something went wrong
# The 'try except' is finished