Skip to content

Commit 214c4a2

Browse files
authored
Merge pull request #5 from ggorman/lect4-widgets-fix
input->widgets + change widgets names
2 parents 25b0fd7 + 4907afd commit 214c4a2

File tree

2 files changed

+211
-153
lines changed

2 files changed

+211
-153
lines changed

notebook/Lecture-4-Introduction-to-programming-for-geoscientists-Solutions.ipynb

Lines changed: 44 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
{
3232
"data": {
3333
"application/vnd.jupyter.widget-view+json": {
34-
"model_id": "73fee2a63e574f2f9b5baa04e0a6c0c7",
34+
"model_id": "77b11537773e4a6fbe4d724af35b7f01",
3535
"version_major": 2,
3636
"version_minor": 0
3737
},
@@ -41,27 +41,20 @@
4141
},
4242
"metadata": {},
4343
"output_type": "display_data"
44-
},
45-
{
46-
"name": "stdout",
47-
"output_type": "stream",
48-
"text": [
49-
"0.0 degrees Fahrenheit is -17.77777777777778 degrees Celsius\n"
50-
]
5144
}
5245
],
5346
"source": [
5447
"from ipywidgets import widgets\n",
5548
"from IPython.display import display\n",
5649
"\n",
57-
"def widget_f2c(sender):\n",
50+
"def f2c(sender):\n",
5851
" F = sender.value # Read the text value; note this is a string\n",
5952
" F = float(F) # Convert this string into a float\n",
6053
" print(F, \"degrees Fahrenheit is \", (F - 32)*(5./9.), \"degrees Celsius\")\n",
6154
"\n",
62-
"temp = widgets.Text()\n",
63-
"temp.on_submit(widget_f2c)\n",
64-
"display(temp)"
55+
"widget_f2c = widgets.Text()\n",
56+
"widget_f2c.on_submit(f2c)\n",
57+
"display(widget_f2c)"
6558
]
6659
},
6760
{
@@ -106,7 +99,7 @@
10699
{
107100
"data": {
108101
"application/vnd.jupyter.widget-view+json": {
109-
"model_id": "48e2409afd454170a60ac756796d2b8a",
102+
"model_id": "15522825c42e4ab1bb41e735e0eb37f0",
110103
"version_major": 2,
111104
"version_minor": 0
112105
},
@@ -116,20 +109,13 @@
116109
},
117110
"metadata": {},
118111
"output_type": "display_data"
119-
},
120-
{
121-
"name": "stdout",
122-
"output_type": "stream",
123-
"text": [
124-
"distance traveled: -3.9050000000000002 meters\n"
125-
]
126112
}
127113
],
128114
"source": [
129115
"from ipywidgets import widgets\n",
130116
"from IPython.display import display\n",
131117
"\n",
132-
"def widget_distance(sender):\n",
118+
"def distance(sender):\n",
133119
" t, v0 = sender.value.split(\",\") # Read the text value; note the use of *split*\n",
134120
" t = float(t)\n",
135121
" v0 = float(v0)\n",
@@ -138,9 +124,9 @@
138124
" y = v0*t - 0.5*g*t**2 # Computes y\n",
139125
" print(\"distance traveled: \", y, \"meters\")\n",
140126
"\n",
141-
"tv0 = widgets.Text()\n",
142-
"tv0.on_submit(widget_distance)\n",
143-
"display(tv0)"
127+
"widget_distance = widgets.Text()\n",
128+
"widget_distance.on_submit(distance)\n",
129+
"display(widget_distance)"
144130
]
145131
},
146132
{
@@ -183,16 +169,16 @@
183169
"from ipywidgets import widgets\n",
184170
"from IPython.display import display\n",
185171
"\n",
186-
"def widget_f2c(sender):\n",
172+
"def f2c(sender):\n",
187173
" F = sender.value # Read the text value; note this is a string\n",
188174
" try:\n",
189175
" F = float(F) # Convert this string into a float\n",
190176
" print(F, \"degrees Fahrenheit is \", (F - 32)*(5./9.), \"degrees Celsius\")\n",
191177
" except:\n",
192178
" print(\"ERROR: invalid input, value entred must be a float (ie a real number)\")\n",
193-
"temp = widgets.Text()\n",
194-
"temp.on_submit(widget_f2c)\n",
195-
"display(temp)"
179+
"widget_f2c = widgets.Text()\n",
180+
"widget_f2c.on_submit(f2c)\n",
181+
"display(widget_f2c)"
196182
]
197183
},
198184
{
@@ -205,24 +191,29 @@
205191
},
206192
{
207193
"cell_type": "code",
208-
"execution_count": 6,
194+
"execution_count": 3,
209195
"metadata": {},
210196
"outputs": [
211197
{
212-
"name": "stdout",
213-
"output_type": "stream",
214-
"text": [
215-
"v0=? 0\n",
216-
"t=? 0\n",
217-
"0.0\n"
218-
]
198+
"data": {
199+
"application/vnd.jupyter.widget-view+json": {
200+
"model_id": "14468710b70a402fab9ff7e0160ba3c8",
201+
"version_major": 2,
202+
"version_minor": 0
203+
},
204+
"text/plain": [
205+
"A Jupyter Widget"
206+
]
207+
},
208+
"metadata": {},
209+
"output_type": "display_data"
219210
}
220211
],
221212
"source": [
222213
"from ipywidgets import widgets\n",
223214
"from IPython.display import display\n",
224215
"\n",
225-
"def widget_distance(sender):\n",
216+
"def distance(sender):\n",
226217
" try:\n",
227218
" t, v0 = sender.value.split(\",\") # Read the text value; note the use of *split*\n",
228219
" except:\n",
@@ -240,9 +231,9 @@
240231
" y = v0*t - 0.5*g*t**2 # Computes y\n",
241232
" print(\"distance traveled: \", y, \"meters\")\n",
242233
"\n",
243-
"tv0 = widgets.Text()\n",
244-
"tv0.on_submit(widget_distance)\n",
245-
"display(tv0)"
234+
"widget_distance = widgets.Text()\n",
235+
"widget_distance.on_submit(distance)\n",
236+
"display(widget_distance)"
246237
]
247238
},
248239
{
@@ -255,13 +246,13 @@
255246
},
256247
{
257248
"cell_type": "code",
258-
"execution_count": 5,
249+
"execution_count": 4,
259250
"metadata": {},
260251
"outputs": [
261252
{
262253
"data": {
263254
"application/vnd.jupyter.widget-view+json": {
264-
"model_id": "9833d30264f7420f8e9a3f72b3f087da",
255+
"model_id": "6853dfbf6d46489e9bdc1938688794ee",
265256
"version_major": 2,
266257
"version_minor": 0
267258
},
@@ -271,22 +262,13 @@
271262
},
272263
"metadata": {},
273264
"output_type": "display_data"
274-
},
275-
{
276-
"name": "stdout",
277-
"output_type": "stream",
278-
"text": [
279-
"ERROR: value of t must be between 0 and 0.2038735983690112\n",
280-
"ERROR: value of t must be between 0 and 0.040774719673802244\n",
281-
"distance traveled: 0.050949999999999995 meters\n"
282-
]
283265
}
284266
],
285267
"source": [
286268
"from ipywidgets import widgets\n",
287269
"from IPython.display import display\n",
288270
"\n",
289-
"def widget_distance(sender):\n",
271+
"def distance(sender):\n",
290272
" g = 9.81 # Assigns g value\n",
291273
"\n",
292274
" try:\n",
@@ -312,9 +294,9 @@
312294
" y = v0*t - 0.5*g*t**2 # Computes y\n",
313295
" print(\"distance traveled: \", y, \"meters\")\n",
314296
"\n",
315-
"tv0 = widgets.Text()\n",
316-
"tv0.on_submit(widget_distance)\n",
317-
"display(tv0)"
297+
"widget_distance = widgets.Text()\n",
298+
"widget_distance.on_submit(distance)\n",
299+
"display(widget_distance)"
318300
]
319301
},
320302
{
@@ -330,13 +312,13 @@
330312
},
331313
{
332314
"cell_type": "code",
333-
"execution_count": 7,
315+
"execution_count": 5,
334316
"metadata": {},
335317
"outputs": [
336318
{
337319
"data": {
338320
"application/vnd.jupyter.widget-view+json": {
339-
"model_id": "f75a143209ec45c39231b0109672d117",
321+
"model_id": "c066db4a33354184a56c3afee4511a88",
340322
"version_major": 2,
341323
"version_minor": 0
342324
},
@@ -346,20 +328,13 @@
346328
},
347329
"metadata": {},
348330
"output_type": "display_data"
349-
},
350-
{
351-
"name": "stdout",
352-
"output_type": "stream",
353-
"text": [
354-
"breaking distance: 188.77185034167707 meters\n"
355-
]
356331
}
357332
],
358333
"source": [
359334
"from ipywidgets import widgets\n",
360335
"from IPython.display import display\n",
361336
"\n",
362-
"def widget_distance(sender):\n",
337+
"def distance(sender):\n",
363338
" g = 9.81 # Assigns g value\n",
364339
"\n",
365340
" try:\n",
@@ -380,9 +355,9 @@
380355
"\n",
381356
" print(\"breaking distance: \", d, \"meters\")\n",
382357
"\n",
383-
"v0mu = widgets.Text()\n",
384-
"v0mu.on_submit(widget_distance)\n",
385-
"display(v0mu)"
358+
"widget_distance = widgets.Text()\n",
359+
"widget_distance.on_submit(distance)\n",
360+
"display(widget_distance)"
386361
]
387362
},
388363
{
@@ -411,7 +386,7 @@
411386
"name": "python",
412387
"nbconvert_exporter": "python",
413388
"pygments_lexer": "ipython3",
414-
"version": "3.6.2"
389+
"version": "3.5.2"
415390
}
416391
},
417392
"nbformat": 4,

0 commit comments

Comments
 (0)