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
Auto-generated via `{sandpaper}`
Source : 312e3c3
Branch : main
Author : scottan <33283688+Scottan@users.noreply.github.com>
Time : 2026-04-13 12:47:22 +0000
Message : Merge pull request #56 from UoMResearchIT/21-make-errors-example-into-a-function
21 make errors example into a function
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,21 +130,18 @@ 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)
134
+
reciprocal =1/val
139
135
exceptTypeErroras err:
140
136
print('Val is not a number')
141
-
print('But our code does not crash anymore')
137
+
print('The run-time error is:', err)
138
+
exceptExceptionas err:
139
+
print('Some error other than a TypeError occured')
142
140
print('The run-time error is:', err)
143
141
else:
144
-
print('1/val = ', 1/val)
142
+
print('The reciprocal of the value =', reciprocal)
145
143
finally:
146
144
print('release memory')
147
-
del(val)
148
145
```
149
146
150
147
The typical use of the `finally` statement is to deal with the release of external resources (such as files or network connections) whether or not the attempted action has been successful.
@@ -219,12 +216,7 @@ val = 'a'
219
216
220
217
asserttype(val) isfloatortype(val) isint, "Variable has to be a numerical object"
221
218
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.')
219
+
check_sign(val)
228
220
```
229
221
230
222
```output
@@ -252,12 +244,7 @@ val = np.nan
252
244
253
245
asserttype(val) isfloatortype(val) isint, "Variable has to be a numerical object"
254
246
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.')
247
+
check_sign(val)
261
248
```
262
249
263
250
```output
@@ -281,12 +268,7 @@ val = np.nan
281
268
asserttype(val) isfloatortype(val) isint, "Variable has to be a numerical object"
282
269
assertnot np.isnan(val), "Variable must not be a NaN"
0 commit comments