|
8 | 8 | {
|
9 | 9 | "cells": [
|
10 | 10 | {
|
11 |
| - "cell_type": "heading", |
12 |
| - "level": 1, |
| 11 | + "cell_type": "markdown", |
13 | 12 | "metadata": {},
|
14 | 13 | "source": [
|
15 |
| - "Introduction to programming for Geoscientists (through Python)" |
| 14 | + "#Introduction to programming for Geoscientists (through Python)\n", |
| 15 | + "###[Gerard Gorman](http://www.imperial.ac.uk/people/g.gorman), [Christian Jacobs](http://www.imperial.ac.uk/people/c.jacobs10)" |
16 | 16 | ]
|
17 | 17 | },
|
18 | 18 | {
|
|
21 | 21 | "source": [
|
22 | 22 | "#Lecture 3: Input data and error handling\n",
|
23 | 23 | "\n",
|
24 |
| - "[Gerard J. Gorman](http://www.imperial.ac.uk/people/g.gorman) <g.gorman@imperial.ac.uk>\n", |
25 |
| - "\n", |
26 |
| - "Learning objectives: You will learn how to read input data into a Python program. You will be able to catch run-time errors and handle them gracefully rather than the program simply failing." |
| 24 | + "Learning objectives:\n", |
| 25 | + "* Learn how to read input data into a Python program.\n", |
| 26 | + "* Be able to catch run-time errors and handle them gracefully rather than the program simply failing." |
27 | 27 | ]
|
28 | 28 | },
|
29 | 29 | {
|
|
324 | 324 | ],
|
325 | 325 | "prompt_number": 8
|
326 | 326 | },
|
| 327 | + { |
| 328 | + "cell_type": "markdown", |
| 329 | + "metadata": {}, |
| 330 | + "source": [ |
| 331 | + "## <span style=\"color:blue\">Exercise 1: Make an interactive program</span>\n", |
| 332 | + "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." |
| 333 | + ] |
| 334 | + }, |
| 335 | + { |
| 336 | + "cell_type": "code", |
| 337 | + "collapsed": false, |
| 338 | + "input": [], |
| 339 | + "language": "python", |
| 340 | + "metadata": {}, |
| 341 | + "outputs": [] |
| 342 | + }, |
| 343 | + { |
| 344 | + "cell_type": "markdown", |
| 345 | + "metadata": {}, |
| 346 | + "source": [ |
| 347 | + "## <span style=\"color:blue\">Exercise 2: Prompt the user for input to a formula</span>\n", |
| 348 | + "Consider the simplest program for evaluting the formula $y(t) = v_0 t \u2212 0.5gt^2$:" |
| 349 | + ] |
| 350 | + }, |
| 351 | + { |
| 352 | + "cell_type": "code", |
| 353 | + "collapsed": false, |
| 354 | + "input": [ |
| 355 | + "v0 = 3; g = 9.81; t = 0.6\n", |
| 356 | + "y = v0*t - 0.5*g*t**2\n", |
| 357 | + "print y" |
| 358 | + ], |
| 359 | + "language": "python", |
| 360 | + "metadata": {}, |
| 361 | + "outputs": [] |
| 362 | + }, |
| 363 | + { |
| 364 | + "cell_type": "markdown", |
| 365 | + "metadata": {}, |
| 366 | + "source": [ |
| 367 | + "Modify this code so that the program asks the user questions *t=?* and *v0=?*, and then gets *t* and *v0* from the user's input through the keyboard." |
| 368 | + ] |
| 369 | + }, |
327 | 370 | {
|
328 | 371 | "cell_type": "markdown",
|
329 | 372 | "metadata": {},
|
|
629 | 672 | }
|
630 | 673 | ],
|
631 | 674 | "prompt_number": 16
|
| 675 | + }, |
| 676 | + { |
| 677 | + "cell_type": "markdown", |
| 678 | + "metadata": {}, |
| 679 | + "source": [ |
| 680 | + "## <span style=\"color:blue\">Exercise 3: Use exceptions</span>\n", |
| 681 | + "Extend the program from Exercise 1 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." |
| 682 | + ] |
| 683 | + }, |
| 684 | + { |
| 685 | + "cell_type": "code", |
| 686 | + "collapsed": false, |
| 687 | + "input": [], |
| 688 | + "language": "python", |
| 689 | + "metadata": {}, |
| 690 | + "outputs": [] |
| 691 | + }, |
| 692 | + { |
| 693 | + "cell_type": "markdown", |
| 694 | + "metadata": {}, |
| 695 | + "source": [ |
| 696 | + "## <span style=\"color:blue\">Exercise 4a: Make the program from Exercise 2 safer</span>\n", |
| 697 | + "Extend the program from Exercise 2 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." |
| 698 | + ] |
| 699 | + }, |
| 700 | + { |
| 701 | + "cell_type": "code", |
| 702 | + "collapsed": false, |
| 703 | + "input": [], |
| 704 | + "language": "python", |
| 705 | + "metadata": {}, |
| 706 | + "outputs": [] |
| 707 | + }, |
| 708 | + { |
| 709 | + "cell_type": "markdown", |
| 710 | + "metadata": {}, |
| 711 | + "source": [ |
| 712 | + "## <span style=\"color:blue\">Exercise 4b: Test more in the program</span>\n", |
| 713 | + "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." |
| 714 | + ] |
| 715 | + }, |
| 716 | + { |
| 717 | + "cell_type": "code", |
| 718 | + "collapsed": false, |
| 719 | + "input": [], |
| 720 | + "language": "python", |
| 721 | + "metadata": {}, |
| 722 | + "outputs": [] |
| 723 | + }, |
| 724 | + { |
| 725 | + "cell_type": "markdown", |
| 726 | + "metadata": {}, |
| 727 | + "source": [ |
| 728 | + "## <span style=\"color:blue\">Exercise 4c: Raising an exception</span>\n", |
| 729 | + "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." |
| 730 | + ] |
| 731 | + }, |
| 732 | + { |
| 733 | + "cell_type": "code", |
| 734 | + "collapsed": false, |
| 735 | + "input": [], |
| 736 | + "language": "python", |
| 737 | + "metadata": {}, |
| 738 | + "outputs": [] |
| 739 | + }, |
| 740 | + { |
| 741 | + "cell_type": "markdown", |
| 742 | + "metadata": {}, |
| 743 | + "source": [ |
| 744 | + "## <span style=\"color:blue\">Exercise 5: Compute the distance it takes to stop a car</span>\n", |
| 745 | + "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", |
| 746 | + "$d = 0.5\\frac{v_0^2}{\\mu g}$</br>\n", |
| 747 | + "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", |
| 748 | + "program for two cases: $v_0$ = 120 and $v_0$ = 50 km/h, both with $\\mu$ = 0.3 ($\\mu$ is dimensionless). (Remember to convert the velocity from km/h to m/s before inserting the value in the formula!)" |
| 749 | + ] |
| 750 | + }, |
| 751 | + { |
| 752 | + "cell_type": "code", |
| 753 | + "collapsed": false, |
| 754 | + "input": [], |
| 755 | + "language": "python", |
| 756 | + "metadata": {}, |
| 757 | + "outputs": [] |
632 | 758 | }
|
633 | 759 | ],
|
634 | 760 | "metadata": {}
|
|
0 commit comments