You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We can avoid problems like this by wrapping our code in an `if` statement:
78
+
We can avoid problems like this by wrapping our code in an `if` statement.
79
+
To make things simpler, we will first write the test as a function:
79
80
80
81
```python
81
-
if type(val) is int or type(val) is float:
82
+
def check_value(val):
82
83
if val>0 and val<10:
83
84
print('Value: ', val, 'is a digit.')
84
85
elif val==0:
85
86
print('Value ', val, 'is nul')
86
87
else:
87
88
print('Value: ', val, 'is a number.')
89
+
```
90
+
91
+
Then wrap the functioncallin an `if` statement:
92
+
93
+
```python
94
+
if type(val) is int or type(val) is float:
95
+
check_value(val)
88
96
else:
89
97
print('val is not a number')
90
98
```
@@ -95,29 +103,21 @@ Python provides the `try-except` structure to avoid this issue, enabling develop
95
103
96
104
```python
97
105
try:
98
-
if val>0 and val<10:
99
-
print('Value: ', val, 'is a digit.')
100
-
elif val==0:
101
-
print('Value ', val, 'is nul')
102
-
else:
103
-
print('Value: ', val, 'is a number.')
106
+
check_value(val)
104
107
except:
105
108
print('Val is not a number')
106
109
print('Enter a new number')
107
110
```
108
111
109
112
At the top of the statement is the code that we are interested in executing, which is run in the `try` statement. If that fails then the `except` statement comes into effect, (hopefully) returning helpful information to the user about what happened and giving them some guidance on how to avoid the problem in future.
110
113
114
+
Using `try-except` statements results in clearer, easier to understand code by following the common Python coding style of [EAFP](https://docs.python.org/3.6/glossary.html#term-eafp) (it's easier to ask for forgiveness than permission). This style shows the code we want to execute first, assuming that the incoming data is correct, before dealing with exceptions if the assumptions prove false.
115
+
111
116
The `except` statement will catch all errors and so we do not, initially at least, need to know exactly what errors we are trying to avoid. However, python does provide error codes, which we can use to expand the structure to capture specific error types. For the example above, we would want to capture a `TypeError`:
112
117
113
118
```python
114
119
try:
115
-
if val>0 and val<10:
116
-
print('Value: ', val, 'is a digit.')
117
-
elif val==0:
118
-
print('Value ', val, 'is nul')
119
-
else:
120
-
print('Value: ', val, 'is a number.')
120
+
check_value(val)
121
121
except TypeError as err:
122
122
print('Val is not a number')
123
123
print('But our code does not crash anymore')
@@ -130,12 +130,7 @@ As with `if` statements, multiple `except` statements can be used, each with a d
130
130
131
131
```python
132
132
try:
133
-
if val>0 and val<10:
134
-
print('Value: ', val, 'is a digit.')
135
-
elif val==0:
136
-
print('Value ', val, 'is nul')
137
-
else:
138
-
print('Value: ', val, 'is a number.')
133
+
check_value(val)
139
134
except TypeError as err:
140
135
print('Val is not a number')
141
136
print('But our code does not crash anymore')
@@ -219,12 +214,7 @@ val = 'a'
219
214
220
215
assert type(val) is float or type(val) is int, "Variable has to be a numerical object"
221
216
222
-
if val>0 and val<10:
223
-
print('Value: ', val, 'is a digit.')
224
-
elif val==0:
225
-
print('Value ', val, 'is nul')
226
-
else:
227
-
print('Value: ', val, 'is a number.')
217
+
check_value(val)
228
218
```
229
219
230
220
```output
@@ -252,12 +242,7 @@ val = np.nan
252
242
253
243
assert type(val) is float or type(val) is int, "Variable has to be a numerical object"
254
244
255
-
if val>0 and val<10:
256
-
print('Value: ', val, 'is a digit.')
257
-
elif val==0:
258
-
print('Value ', val, 'is nul')
259
-
else:
260
-
print('Value: ', val, 'is a number.')
245
+
check_value(val)
261
246
```
262
247
263
248
```output
@@ -281,12 +266,7 @@ val = np.nan
281
266
assert type(val) is float or type(val) is int, "Variable has to be a numerical object"
282
267
assert not np.isnan(val), "Variable must not be a NaN"
0 commit comments