Skip to content

Commit 4f7114a

Browse files
committed
adds mynum
1 parent d11c857 commit 4f7114a

File tree

2 files changed

+126
-22
lines changed

2 files changed

+126
-22
lines changed

02-assertions.ipynb

Lines changed: 125 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
},
1818
{
1919
"cell_type": "code",
20-
"execution_count": null,
20+
"execution_count": 2,
2121
"metadata": {
2222
"collapsed": true
2323
},
@@ -29,25 +29,49 @@
2929
},
3030
{
3131
"cell_type": "code",
32-
"execution_count": null,
32+
"execution_count": 3,
3333
"metadata": {
3434
"collapsed": false,
3535
"scrolled": true
3636
},
37-
"outputs": [],
37+
"outputs": [
38+
{
39+
"ename": "AssertionError",
40+
"evalue": "",
41+
"output_type": "error",
42+
"traceback": [
43+
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
44+
"\u001b[0;31mAssertionError\u001b[0m Traceback (most recent call last)",
45+
"\u001b[0;32m<ipython-input-3-e5d2d9c5d319>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# when the argument is false, an assertion error is raised\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0;32massert\u001b[0m \u001b[0;32mTrue\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;32mFalse\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
46+
"\u001b[0;31mAssertionError\u001b[0m: "
47+
]
48+
}
49+
],
3850
"source": [
3951
"# when the argument is false, an assertion error is raised\n",
4052
"assert True == False"
4153
]
4254
},
4355
{
4456
"cell_type": "code",
45-
"execution_count": null,
57+
"execution_count": 4,
4658
"metadata": {
4759
"collapsed": false,
4860
"scrolled": true
4961
},
50-
"outputs": [],
62+
"outputs": [
63+
{
64+
"ename": "AssertionError",
65+
"evalue": "True is not False",
66+
"output_type": "error",
67+
"traceback": [
68+
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
69+
"\u001b[0;31mAssertionError\u001b[0m Traceback (most recent call last)",
70+
"\u001b[0;32m<ipython-input-4-5c334bceab7f>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# an error message can even be added to the assertion\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0;32massert\u001b[0m \u001b[0;32mTrue\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;32mFalse\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"True is not False\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
71+
"\u001b[0;31mAssertionError\u001b[0m: True is not False"
72+
]
73+
}
74+
],
5175
"source": [
5276
"# an error message can even be added to the assertion\n",
5377
"assert True == False, \"True is not False\""
@@ -64,11 +88,24 @@
6488
},
6589
{
6690
"cell_type": "code",
67-
"execution_count": null,
91+
"execution_count": 5,
6892
"metadata": {
6993
"collapsed": false
7094
},
71-
"outputs": [],
95+
"outputs": [
96+
{
97+
"ename": "ZeroDivisionError",
98+
"evalue": "division by zero",
99+
"output_type": "error",
100+
"traceback": [
101+
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
102+
"\u001b[0;31mZeroDivisionError\u001b[0m Traceback (most recent call last)",
103+
"\u001b[0;32m<ipython-input-5-78dbe8a12ddf>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0msum\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnum_list\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m/\u001b[0m\u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnum_list\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 4\u001b[0;31m \u001b[0mmean\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
104+
"\u001b[0;32m<ipython-input-5-78dbe8a12ddf>\u001b[0m in \u001b[0;36mmean\u001b[0;34m(num_list)\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mmean\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnum_list\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0msum\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnum_list\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m/\u001b[0m\u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnum_list\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 3\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0mmean\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
105+
"\u001b[0;31mZeroDivisionError\u001b[0m: division by zero"
106+
]
107+
}
108+
],
72109
"source": [
73110
"def mean(num_list):\n",
74111
" return sum(num_list)/len(num_list)\n",
@@ -85,14 +122,27 @@
85122
},
86123
{
87124
"cell_type": "code",
88-
"execution_count": null,
125+
"execution_count": 6,
89126
"metadata": {
90127
"collapsed": false
91128
},
92-
"outputs": [],
129+
"outputs": [
130+
{
131+
"ename": "AssertionError",
132+
"evalue": "the input is empty but should be a list of numbers",
133+
"output_type": "error",
134+
"traceback": [
135+
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
136+
"\u001b[0;31mAssertionError\u001b[0m Traceback (most recent call last)",
137+
"\u001b[0;32m<ipython-input-6-00b3a334fbad>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0msum\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnum_list\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m/\u001b[0m\u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnum_list\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 5\u001b[0;31m \u001b[0mmean\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
138+
"\u001b[0;32m<ipython-input-6-00b3a334fbad>\u001b[0m in \u001b[0;36mmean\u001b[0;34m(num_list)\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mmean\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnum_list\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0;32massert\u001b[0m \u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnum_list\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m!=\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"the input is empty but should be a list of numbers\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 3\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0msum\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnum_list\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m/\u001b[0m\u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnum_list\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0mmean\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
139+
"\u001b[0;31mAssertionError\u001b[0m: the input is empty but should be a list of numbers"
140+
]
141+
}
142+
],
93143
"source": [
94144
"def mean(num_list):\n",
95-
" assert len(num_list) != 0, \"the input is empty\"\n",
145+
" assert len(num_list) != 0, \"the input is empty but should be a list of numbers\"\n",
96146
" return sum(num_list)/len(num_list)\n",
97147
"\n",
98148
"mean([])"
@@ -107,11 +157,24 @@
107157
},
108158
{
109159
"cell_type": "code",
110-
"execution_count": null,
160+
"execution_count": 7,
111161
"metadata": {
112162
"collapsed": false
113163
},
114-
"outputs": [],
164+
"outputs": [
165+
{
166+
"ename": "TypeError",
167+
"evalue": "unsupported operand type(s) for +: 'int' and 'str'",
168+
"output_type": "error",
169+
"traceback": [
170+
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
171+
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
172+
"\u001b[0;32m<ipython-input-7-b40137885f93>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mmean\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"nonempty-nonlist\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
173+
"\u001b[0;32m<ipython-input-6-00b3a334fbad>\u001b[0m in \u001b[0;36mmean\u001b[0;34m(num_list)\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mmean\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnum_list\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;32massert\u001b[0m \u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnum_list\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m!=\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"the input is empty but should be a list of numbers\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0msum\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnum_list\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m/\u001b[0m\u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnum_list\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 4\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0mmean\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
174+
"\u001b[0;31mTypeError\u001b[0m: unsupported operand type(s) for +: 'int' and 'str'"
175+
]
176+
}
177+
],
115178
"source": [
116179
"mean(\"nonempty-nonlist\")"
117180
]
@@ -127,49 +190,87 @@
127190
},
128191
{
129192
"cell_type": "code",
130-
"execution_count": null,
193+
"execution_count": 8,
131194
"metadata": {
132195
"collapsed": false
133196
},
134197
"outputs": [],
135198
"source": [
136199
"def mean(num_list):\n",
137200
" assert len(num_list) != 0, \"the input is empty\"\n",
201+
" assert isinstance(num_list, list), \"the input must be a list\"\n",
138202
" # insert your assertion here\n",
139203
" return sum(num_list)/len(num_list)"
140204
]
141205
},
142206
{
143207
"cell_type": "code",
144-
"execution_count": null,
208+
"execution_count": 9,
145209
"metadata": {
146210
"collapsed": false
147211
},
148-
"outputs": [],
212+
"outputs": [
213+
{
214+
"data": {
215+
"text/plain": [
216+
"2.0"
217+
]
218+
},
219+
"execution_count": 9,
220+
"metadata": {},
221+
"output_type": "execute_result"
222+
}
223+
],
149224
"source": [
150225
"# Does it give the right answer?\n",
151226
"mean([1,2,3])"
152227
]
153228
},
154229
{
155230
"cell_type": "code",
156-
"execution_count": null,
231+
"execution_count": 10,
157232
"metadata": {
158233
"collapsed": false
159234
},
160-
"outputs": [],
235+
"outputs": [
236+
{
237+
"ename": "AssertionError",
238+
"evalue": "the input must be a list",
239+
"output_type": "error",
240+
"traceback": [
241+
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
242+
"\u001b[0;31mAssertionError\u001b[0m Traceback (most recent call last)",
243+
"\u001b[0;32m<ipython-input-10-97e4976488ed>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# Does this cause it to halt with a type message?\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mmean\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"test\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
244+
"\u001b[0;32m<ipython-input-8-95737c8c1621>\u001b[0m in \u001b[0;36mmean\u001b[0;34m(num_list)\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mmean\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnum_list\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;32massert\u001b[0m \u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnum_list\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m!=\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"the input is empty\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0;32massert\u001b[0m \u001b[0misinstance\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnum_list\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mlist\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"the input must be a list\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 4\u001b[0m \u001b[0;31m# insert your assertion here\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0msum\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnum_list\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m/\u001b[0m\u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnum_list\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
245+
"\u001b[0;31mAssertionError\u001b[0m: the input must be a list"
246+
]
247+
}
248+
],
161249
"source": [
162250
"# Does this cause it to halt with a type message?\n",
163251
"mean(\"test\")"
164252
]
165253
},
166254
{
167255
"cell_type": "code",
168-
"execution_count": null,
256+
"execution_count": 11,
169257
"metadata": {
170258
"collapsed": false
171259
},
172-
"outputs": [],
260+
"outputs": [
261+
{
262+
"ename": "AssertionError",
263+
"evalue": "the input is empty",
264+
"output_type": "error",
265+
"traceback": [
266+
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
267+
"\u001b[0;31mAssertionError\u001b[0m Traceback (most recent call last)",
268+
"\u001b[0;32m<ipython-input-11-8f75ac94dc5e>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# Does this cause it to halt with an emptiness message?\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mmean\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
269+
"\u001b[0;32m<ipython-input-8-95737c8c1621>\u001b[0m in \u001b[0;36mmean\u001b[0;34m(num_list)\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mmean\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnum_list\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0;32massert\u001b[0m \u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnum_list\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m!=\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"the input is empty\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 3\u001b[0m \u001b[0;32massert\u001b[0m \u001b[0misinstance\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnum_list\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mlist\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"the input must be a list\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0;31m# insert your assertion here\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0msum\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnum_list\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m/\u001b[0m\u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnum_list\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
270+
"\u001b[0;31mAssertionError\u001b[0m: the input is empty"
271+
]
272+
}
273+
],
173274
"source": [
174275
"# Does this cause it to halt with an emptiness message?\n",
175276
"mean([])"
@@ -193,16 +294,18 @@
193294
},
194295
{
195296
"cell_type": "code",
196-
"execution_count": null,
297+
"execution_count": 13,
197298
"metadata": {
198299
"collapsed": false
199300
},
200301
"outputs": [],
201302
"source": [
202303
"from mynum import a\n",
203-
"# a is greater than 2 assertion here\n",
304+
"# a is greater or equal to 2 assertion here\n",
305+
"assert a >= 2\n",
204306
"# a is within 2 decimal places of 2 assertion here\n",
205-
"# a is within 0.003 of 2 assertion here"
307+
"assert \n",
308+
"# a is within 0.003 of 2 assertion here\n"
206309
]
207310
},
208311
{

mynum.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
a = 401.0/200.0

0 commit comments

Comments
 (0)