@@ -30,15 +30,15 @@ More importantly, applications are not expected to crash and we are going to lea
3030
3131Please look at the following code. Can you find the fundamental problem in this test?
3232
33- ``` bash
33+ ``` python
3434val = 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. ' )
4040else :
41- print(' Value: ' , val, ' is a number .' )
41+ print (' Value: ' , val, ' is negative .' )
4242```
4343
4444::::::::::::::: solution
@@ -47,15 +47,15 @@ else:
4747
4848The test assumes that ` val ` is a number, and throws an uncontrolled error if it is not.
4949
50- ` ` ` bash
50+ ``` python
5151val = ' 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. ' )
5757else :
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
7171TypeError: '>' 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.
7979To 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
9191Then wrap the function call in an ` if ` statement:
9292
9393``` python
9494if type (val) is int or type (val) is float :
95- check_value (val)
95+ check_sign (val)
9696else :
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
105105try :
106- check_value (val)
106+ check_sign (val)
107107except :
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
119119try :
120- check_value (val)
120+ check_sign (val)
121121except 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
132132try :
133- check_value (val)
133+ check_sign (val)
134134except 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
215215assert 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
243243assert 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
266266assert type (val) is float or type (val) is int , " Variable has to be a numerical object"
267267assert 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