Skip to content

Commit 0c06596

Browse files
committed
Fix labeling
1 parent 46aa960 commit 0c06596

File tree

1 file changed

+34
-33
lines changed

1 file changed

+34
-33
lines changed

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

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"metadata": {
3-
"name": "Lecture-3-Introduction-to-programming-for-geoscientists-Solutions"
3+
"name": "",
4+
"signature": "sha256:438038df1bfccee5e3a89031c69f3fbaa0fc59576b81919b4e77836101642597"
45
},
56
"nbformat": 3,
67
"nbformat_minor": 0,
@@ -27,7 +28,7 @@
2728
"cell_type": "markdown",
2829
"metadata": {},
2930
"source": [
30-
"* **Make an interactive program**</br>\n",
31+
"* **Exercise 1: Make an interactive program**</br>\n",
3132
"Make a program that (i) asks the user for a temperature in Fahrenheit degrees and reads the number; (ii) computes the corresponding temperature in Celsius degrees; and (iii) prints out the temperature in the Celsius scale."
3233
]
3334
},
@@ -49,33 +50,7 @@
4950
"cell_type": "markdown",
5051
"metadata": {},
5152
"source": [
52-
"* **Use exceptions**</br>\n",
53-
"Extend the program from the previous exercise with a try-except block to handle the potential error that the user enters nothing (or invalid data such as a letter) for the Fahrenheit temperature."
54-
]
55-
},
56-
{
57-
"cell_type": "code",
58-
"collapsed": false,
59-
"input": [
60-
"# Use exceptions\n",
61-
"\n",
62-
"try: # Section of code to try\n",
63-
" F = float(raw_input(\"C=? \")) # Input F becomes a float\n",
64-
"except: # Code executed if there is an error in input (e.g. no input, a letter, etc.)\n",
65-
" print \"ERROR: You must enter a valid Fahrenheit temperature\" # Error message to user\n",
66-
"\n",
67-
"C = (F - 32)*(5./9.) # Computes temperature in Celsius\n",
68-
"print \"%.1f degrees Fahrenheit is %.1f degrees Celsius\" % (F, C) # Prints computed value"
69-
],
70-
"language": "python",
71-
"metadata": {},
72-
"outputs": []
73-
},
74-
{
75-
"cell_type": "markdown",
76-
"metadata": {},
77-
"source": [
78-
"* **Prompt the user for input to a formula**</br>\n",
53+
"* **Exercise 2: Prompt the user for input to a formula**</br>\n",
7954
"Consider the simplest program for evaluting the formula $y(t) = v_0 t \u2212 0.5gt^2$:"
8055
]
8156
},
@@ -129,7 +104,33 @@
129104
"cell_type": "markdown",
130105
"metadata": {},
131106
"source": [
132-
"* **Make the program safer**</br>\n",
107+
"* **Exercise 3 - Use exceptions**</br>\n",
108+
"Extend the program from the previous exercise with a try-except block to handle the potential error that the user enters nothing (or invalid data such as a letter) for the Fahrenheit temperature."
109+
]
110+
},
111+
{
112+
"cell_type": "code",
113+
"collapsed": false,
114+
"input": [
115+
"# Use exceptions\n",
116+
"\n",
117+
"try: # Section of code to try\n",
118+
" F = float(raw_input(\"C=? \")) # Input F becomes a float\n",
119+
"except: # Code executed if there is an error in input (e.g. no input, a letter, etc.)\n",
120+
" print \"ERROR: You must enter a valid Fahrenheit temperature\" # Error message to user\n",
121+
"\n",
122+
"C = (F - 32)*(5./9.) # Computes temperature in Celsius\n",
123+
"print \"%.1f degrees Fahrenheit is %.1f degrees Celsius\" % (F, C) # Prints computed value"
124+
],
125+
"language": "python",
126+
"metadata": {},
127+
"outputs": []
128+
},
129+
{
130+
"cell_type": "markdown",
131+
"metadata": {},
132+
"source": [
133+
"* **Exercise 4a: Make the program safer**</br>\n",
133134
"Extend the program from the previous exercise to include exception handling such that missing (or invalid) values for *t* and *v0* are detected. In the *except ValueError* block, use the raw_input function to ask the user for input data once more."
134135
]
135136
},
@@ -160,7 +161,7 @@
160161
"cell_type": "markdown",
161162
"metadata": {},
162163
"source": [
163-
"* **Test more in the program**</br>\n",
164+
"* **Exercise 4b: Test more in the program**</br>\n",
164165
"Test if the *t* value read in the program from the previous exercise lies between $0$ and ${2v_0}/{g}$. If not, print a message and abort execution."
165166
]
166167
},
@@ -197,7 +198,7 @@
197198
"cell_type": "markdown",
198199
"metadata": {},
199200
"source": [
200-
"* **Raise an exception**</br>\n",
201+
"* **Exercise 4c: Raise an exception**</br>\n",
201202
"Instead of printing an error message and aborting the program explicitly, raise a *ValueError* exception in the *if* test on legal *t* values in the program from the previous exercise. Include the legal interval for *t* in the exception message."
202203
]
203204
},
@@ -233,7 +234,7 @@
233234
"cell_type": "markdown",
234235
"metadata": {},
235236
"source": [
236-
"* **Compute the distance it takes to stop a car**</br>\n",
237+
"* **Exercise 5: Compute the distance it takes to stop a car**</br>\n",
237238
"A car driver, driving at velocity $v_0$, suddenly puts on the brake. What braking distance $d$ is needed to stop the car? One can derive, from basic physics, that</br>\n",
238239
"$d = 0.5\\frac{v_0^2}{\\mu g}$</br>\n",
239240
"Make a program for computing $d$ using the above formula when the initial car velocity $v_0$ and the friction coefficient $\\mu$ are provided via the raw_input function. Run the\n",

0 commit comments

Comments
 (0)