|
21 | 21 | {
|
22 | 22 | "cell_type": "code",
|
23 | 23 | "execution_count": null,
|
24 |
| - "metadata": {}, |
| 24 | + "metadata": { |
| 25 | + "collapsed": true |
| 26 | + }, |
25 | 27 | "outputs": [],
|
26 | 28 | "source": [
|
27 | 29 | "# This line does not belong to the function \n",
|
|
36 | 38 | {
|
37 | 39 | "cell_type": "code",
|
38 | 40 | "execution_count": null,
|
39 |
| - "metadata": {}, |
| 41 | + "metadata": { |
| 42 | + "collapsed": true |
| 43 | + }, |
40 | 44 | "outputs": [],
|
41 | 45 | "source": [
|
42 | 46 | "def grow(height):\n",
|
|
72 | 76 | {
|
73 | 77 | "cell_type": "code",
|
74 | 78 | "execution_count": null,
|
75 |
| - "metadata": {}, |
| 79 | + "metadata": { |
| 80 | + "collapsed": true |
| 81 | + }, |
76 | 82 | "outputs": [],
|
77 | 83 | "source": [
|
78 | 84 | "def subtract(a, b):\n",
|
|
91 | 97 | {
|
92 | 98 | "cell_type": "code",
|
93 | 99 | "execution_count": null,
|
94 |
| - "metadata": {}, |
| 100 | + "metadata": { |
| 101 | + "collapsed": true |
| 102 | + }, |
95 | 103 | "outputs": [],
|
96 | 104 | "source": [
|
97 | 105 | "spam = 10\n",
|
|
102 | 110 | {
|
103 | 111 | "cell_type": "code",
|
104 | 112 | "execution_count": null,
|
105 |
| - "metadata": {}, |
| 113 | + "metadata": { |
| 114 | + "collapsed": true |
| 115 | + }, |
106 | 116 | "outputs": [],
|
107 | 117 | "source": [
|
108 | 118 | "a = 10\n",
|
|
137 | 147 | {
|
138 | 148 | "cell_type": "code",
|
139 | 149 | "execution_count": null,
|
140 |
| - "metadata": {}, |
| 150 | + "metadata": { |
| 151 | + "collapsed": true |
| 152 | + }, |
141 | 153 | "outputs": [],
|
142 | 154 | "source": [
|
143 | 155 | "def issue_fine(amount = 750):\n",
|
|
159 | 171 | {
|
160 | 172 | "cell_type": "code",
|
161 | 173 | "execution_count": null,
|
162 |
| - "metadata": {}, |
| 174 | + "metadata": { |
| 175 | + "collapsed": true |
| 176 | + }, |
163 | 177 | "outputs": [],
|
164 | 178 | "source": [
|
165 | 179 | "def what_happens_after_return():\n",
|
|
185 | 199 | {
|
186 | 200 | "cell_type": "code",
|
187 | 201 | "execution_count": null,
|
188 |
| - "metadata": {}, |
| 202 | + "metadata": { |
| 203 | + "collapsed": true |
| 204 | + }, |
189 | 205 | "outputs": [],
|
190 | 206 | "source": [
|
191 | 207 | "import requests\n",
|
|
200 | 216 | "for case in cases:\n",
|
201 | 217 | " #your code here"
|
202 | 218 | ]
|
203 |
| - }, |
204 |
| - { |
205 |
| - "cell_type": "markdown", |
206 |
| - "metadata": {}, |
207 |
| - "source": [ |
208 |
| - "## Test Functions\n", |
209 |
| - "\n", |
210 |
| - "We can use test functions to check that our functions work.\n", |
211 |
| - "If everything works, the test function is *quiet*, printing nothing. But if a test fails, the test function yields an `Error`, like `AssertionError`.\n", |
212 |
| - "\n", |
213 |
| - "\n", |
214 |
| - "```\n", |
215 |
| - "def f(x):\n", |
216 |
| - " y = ...\n", |
217 |
| - " return y\n", |
218 |
| - "\n", |
219 |
| - "def test_f():\n", |
220 |
| - " x_value = ...\n", |
221 |
| - " expected = ...\n", |
222 |
| - " computed = f(x_value)\n", |
223 |
| - " tol = 1e-12\n", |
224 |
| - " success = abs(expected - computed) < tol\n", |
225 |
| - " assert success\n", |
226 |
| - "\n", |
227 |
| - "test_f()\n", |
228 |
| - "```\n", |
229 |
| - "\n", |
230 |
| - "The test function has the *same* name as the function to be tested, with the *prefix* `test_`.\n", |
231 |
| - "A test function does *not* have any parameters, nor does it return anything. It uses `assert` to test that the function behaves as it should.\n", |
232 |
| - "\n", |
233 |
| - "### `expected` \n", |
234 |
| - "`expected` is the correct value that the function should return. *Never* use the function to be tested to find `expected`, since that would defeat the purpose of the test.\n", |
235 |
| - "\n", |
236 |
| - "### `computed`\n", |
237 |
| - "`computed` is *always* calculated by the function you are testing. Call the function with the parameters it needs.\n", |
238 |
| - "\n", |
239 |
| - "### `success`\n", |
240 |
| - "`success` is a `Boolean`, that is `True` eller `False`. \n", |
241 |
| - "Here we compare the `expected` value with the `computed` result.\n", |
242 |
| - "If the function returns a `float`, we must check that `expected` and `computed` are within a small tolerance, because floating-point values are imprecise. We will not get into that here.\n", |
243 |
| - "\n", |
244 |
| - "### `assert`\n", |
245 |
| - "`assert` checks that something is `True`.\n", |
246 |
| - "If the expression after `assert` is `True`, nothing happens and the test passes.\n", |
247 |
| - "If the expression is `False`, we get an `AssertionError`, and the test fails.\n", |
248 |
| - "You can have several assertions in the same function.\n" |
249 |
| - ] |
250 |
| - }, |
251 |
| - { |
252 |
| - "cell_type": "code", |
253 |
| - "execution_count": null, |
254 |
| - "metadata": { |
255 |
| - "scrolled": true |
256 |
| - }, |
257 |
| - "outputs": [], |
258 |
| - "source": [ |
259 |
| - "assert True, \"This will not be printed, as you are asserting True\"\n", |
260 |
| - "assert False, \"This will be printed, as you are asserting False\"\n", |
261 |
| - "print(\"This will not be printed, as the line above will terminate the program.\")" |
262 |
| - ] |
263 | 219 | }
|
264 | 220 | ],
|
265 | 221 | "metadata": {
|
|
278 | 234 | "name": "python",
|
279 | 235 | "nbconvert_exporter": "python",
|
280 | 236 | "pygments_lexer": "ipython3",
|
281 |
| - "version": "3.6.8" |
| 237 | + "version": "3.6.3" |
282 | 238 | }
|
283 | 239 | },
|
284 | 240 | "nbformat": 4,
|
|
0 commit comments