Skip to content

Commit bcd4a7c

Browse files
author
Scott Archer-Nicholls
committed
fix: replaced check_value with check_sign function
1 parent 609571e commit bcd4a7c

1 file changed

Lines changed: 28 additions & 28 deletions

File tree

‎episodes/05-defensive_programming.md‎

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,15 @@ More importantly, applications are not expected to crash and we are going to lea
3030

3131
Please look at the following code. Can you find the fundamental problem in this test?
3232

33-
```bash
33+
```python
3434
val = 1
3535

36-
if val>0 and val<10:
37-
print('Value: ', val, 'is a digit.')
38-
elif val==0:
39-
print('Value ', val, 'is nul')
36+
if val > 0:
37+
print('Value: ', val, 'is positive.')
38+
elif val == 0:
39+
print('Value: ', val, 'is zero.')
4040
else:
41-
print('Value: ', val, 'is a number.')
41+
print('Value: ', val, 'is negative.')
4242
```
4343

4444
::::::::::::::: solution
@@ -47,15 +47,15 @@ else:
4747

4848
The test assumes that `val` is a number, and throws an uncontrolled error if it is not.
4949

50-
```bash
50+
```python
5151
val = 'a'
5252

53-
if val>0 and val<10:
54-
print('Value: ', val, 'is a digit.')
55-
elif val==0:
56-
print('Value ', val, 'is nul')
53+
if val > 0:
54+
print('Value: ', val, 'is positive.')
55+
elif val == 0:
56+
print('Value: ', val, 'is zero.')
5757
else:
58-
print('Value: ', val, 'is a number.')
58+
print('Value: ', val, 'is negative.')
5959
```
6060

6161
```output
@@ -64,9 +64,9 @@ TypeError Traceback (most recent call last)
6464
<ipython-input-2-99c0e25bf5e9> in <module>()
6565
1 val = 'a'
6666
2
67-
----> 3 if val>0 and val<10:
68-
4 print('Value: ', val, 'is a digit.')
69-
5 elif val==0:
67+
----> 3 if val > 0:
68+
4 print('Value: ', val, 'is positive.')
69+
5 elif val > 0:
7070
7171
TypeError: '>' not supported between instances of 'str' and 'int'
7272
```
@@ -79,20 +79,20 @@ We can avoid problems like this by wrapping our code in an `if` statement.
7979
To make things simpler, we will first write the test as a function:
8080

8181
```python
82-
def check_value(val):
83-
if val>0 and val<10:
84-
print('Value: ', val, 'is a digit.')
85-
elif val==0:
86-
print('Value ', val, 'is nul')
82+
def check_sign(val):
83+
if val > 0:
84+
print('Value: ', val, 'is positive.')
85+
elif val == 0:
86+
print('Value: ', val, 'is zero.')
8787
else:
88-
print('Value: ', val, 'is a number.')
88+
print('Value: ', val, 'is negative.')
8989
```
9090

9191
Then wrap the function call in an `if` statement:
9292

9393
```python
9494
if type(val) is int or type(val) is float:
95-
check_value(val)
95+
check_sign(val)
9696
else:
9797
print('val is not a number')
9898
```
@@ -103,7 +103,7 @@ Python provides the `try-except` structure to avoid this issue, enabling develop
103103

104104
```python
105105
try:
106-
check_value(val)
106+
check_sign(val)
107107
except:
108108
print('Val is not a number')
109109
print('Enter a new number')
@@ -117,7 +117,7 @@ The `except` statement will catch all errors and so we do not, initially at leas
117117

118118
```python
119119
try:
120-
check_value(val)
120+
check_sign(val)
121121
except TypeError as err:
122122
print('Val is not a number')
123123
print('But our code does not crash anymore')
@@ -130,7 +130,7 @@ As with `if` statements, multiple `except` statements can be used, each with a d
130130

131131
```python
132132
try:
133-
check_value(val)
133+
check_sign(val)
134134
except TypeError as err:
135135
print('Val is not a number')
136136
print('But our code does not crash anymore')
@@ -214,7 +214,7 @@ val = 'a'
214214

215215
assert type(val) is float or type(val) is int, "Variable has to be a numerical object"
216216

217-
check_value(val)
217+
check_sign(val)
218218
```
219219

220220
```output
@@ -242,7 +242,7 @@ val = np.nan
242242

243243
assert type(val) is float or type(val) is int, "Variable has to be a numerical object"
244244

245-
check_value(val)
245+
check_sign(val)
246246
```
247247

248248
```output
@@ -266,7 +266,7 @@ val = np.nan
266266
assert type(val) is float or type(val) is int, "Variable has to be a numerical object"
267267
assert not np.isnan(val), "Variable must not be a NaN"
268268

269-
check_value(val)
269+
check_sign(val)
270270
```
271271

272272
```output

0 commit comments

Comments
 (0)