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
-
if val>0 and val<10:
83
-
print('Value: ', val, 'is a digit.')
84
-
elif val==0:
85
-
print('Value ', val, 'is nul')
82
+
defcheck_sign(val):
83
+
if val>0:
84
+
print('Value: ', val, 'is positive.')
85
+
elif val==0:
86
+
print('Value:', val, 'is zero.')
86
87
else:
87
-
print('Value: ', val, 'is a number.')
88
+
print('Value: ', val, 'is negative.')
89
+
```
90
+
91
+
Then wrap the function call in an `if` statement:
92
+
93
+
```python
94
+
iftype(val) isintortype(val) isfloat:
95
+
check_sign(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_sign(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_sign(val)
121
121
exceptTypeErroras err:
122
122
print('Val is not a number')
123
123
print('But our code does not crash anymore')
@@ -130,18 +130,13 @@ 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_sign(val)
139
134
exceptTypeErroras err:
140
135
print('Val is not a number')
141
136
print('But our code does not crash anymore')
142
137
print('The run-time error is:', err)
143
138
else:
144
-
print('1/val = ', 1/val)
139
+
print('The value provided to the check_sign function did not result in a TypeError')
145
140
finally:
146
141
print('release memory')
147
142
del(val)
@@ -219,12 +214,7 @@ val = 'a'
219
214
220
215
asserttype(val) isfloatortype(val) isint, "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_sign(val)
228
218
```
229
219
230
220
```output
@@ -252,12 +242,7 @@ val = np.nan
252
242
253
243
asserttype(val) isfloatortype(val) isint, "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_sign(val)
261
246
```
262
247
263
248
```output
@@ -281,12 +266,7 @@ val = np.nan
281
266
asserttype(val) isfloatortype(val) isint, "Variable has to be a numerical object"
282
267
assertnot np.isnan(val), "Variable must not be a NaN"
0 commit comments