|
1 | 1 | {
|
2 | 2 | "metadata": {
|
3 |
| - "name": "Lecture-3-Introduction-to-programming-for-geoscientists-Solutions" |
| 3 | + "name": "", |
| 4 | + "signature": "sha256:438038df1bfccee5e3a89031c69f3fbaa0fc59576b81919b4e77836101642597" |
4 | 5 | },
|
5 | 6 | "nbformat": 3,
|
6 | 7 | "nbformat_minor": 0,
|
|
27 | 28 | "cell_type": "markdown",
|
28 | 29 | "metadata": {},
|
29 | 30 | "source": [
|
30 |
| - "* **Make an interactive program**</br>\n", |
| 31 | + "* **Exercise 1: Make an interactive program**</br>\n", |
31 | 32 | "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."
|
32 | 33 | ]
|
33 | 34 | },
|
|
49 | 50 | "cell_type": "markdown",
|
50 | 51 | "metadata": {},
|
51 | 52 | "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", |
79 | 54 | "Consider the simplest program for evaluting the formula $y(t) = v_0 t \u2212 0.5gt^2$:"
|
80 | 55 | ]
|
81 | 56 | },
|
|
129 | 104 | "cell_type": "markdown",
|
130 | 105 | "metadata": {},
|
131 | 106 | "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", |
133 | 134 | "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."
|
134 | 135 | ]
|
135 | 136 | },
|
|
160 | 161 | "cell_type": "markdown",
|
161 | 162 | "metadata": {},
|
162 | 163 | "source": [
|
163 |
| - "* **Test more in the program**</br>\n", |
| 164 | + "* **Exercise 4b: Test more in the program**</br>\n", |
164 | 165 | "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."
|
165 | 166 | ]
|
166 | 167 | },
|
|
197 | 198 | "cell_type": "markdown",
|
198 | 199 | "metadata": {},
|
199 | 200 | "source": [
|
200 |
| - "* **Raise an exception**</br>\n", |
| 201 | + "* **Exercise 4c: Raise an exception**</br>\n", |
201 | 202 | "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."
|
202 | 203 | ]
|
203 | 204 | },
|
|
233 | 234 | "cell_type": "markdown",
|
234 | 235 | "metadata": {},
|
235 | 236 | "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", |
237 | 238 | "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",
|
238 | 239 | "$d = 0.5\\frac{v_0^2}{\\mu g}$</br>\n",
|
239 | 240 | "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