{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Introduction to programming for Geoscientists through Python" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Lecture 4 solutions\n", "## Gerard J. Gorman (g.gorman@imperial.ac.uk) http://www.imperial.ac.uk/people/g.gorman" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* **Exercise 4.1: Make an interactive program**
\n", "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." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "77b11537773e4a6fbe4d724af35b7f01", "version_major": 2, "version_minor": 0 }, "text/plain": [ "A Jupyter Widget" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from ipywidgets import widgets\n", "from IPython.display import display\n", "\n", "def f2c(sender):\n", " F = sender.value # Read the text value; note this is a string\n", " F = float(F) # Convert this string into a float\n", " print(F, \"degrees Fahrenheit is \", (F - 32)*(5./9.), \"degrees Celsius\")\n", "\n", "widget_f2c = widgets.Text()\n", "widget_f2c.on_submit(f2c)\n", "display(widget_f2c)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* **Exercise 4.2: Prompt the user for input to a formula**
\n", "Consider the simplest program for evaluting the formula $y(t) = v_0 t − 0.5gt^2$:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0.034199999999999786\n" ] } ], "source": [ "v0 = 3; g = 9.81; t = 0.6\n", "y = v0*t - 0.5*g*t**2\n", "print(y)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Modify this code so that the program takes as user input *t* and *v0*, and then computes the corresponding distance traveled, $y$. Note, you can put multiple values into the same text box and then use the string method *split* to break the string value into a list." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "15522825c42e4ab1bb41e735e0eb37f0", "version_major": 2, "version_minor": 0 }, "text/plain": [ "A Jupyter Widget" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from ipywidgets import widgets\n", "from IPython.display import display\n", "\n", "def distance(sender):\n", " t, v0 = sender.value.split(\",\") # Read the text value; note the use of *split*\n", " t = float(t)\n", " v0 = float(v0)\n", " \n", " g = 9.81 # Assigns g value\n", " y = v0*t - 0.5*g*t**2 # Computes y\n", " print(\"distance traveled: \", y, \"meters\")\n", "\n", "widget_distance = widgets.Text()\n", "widget_distance.on_submit(distance)\n", "display(widget_distance)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* **Exercise 4.4 - Use exceptions**
\n", "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." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "3042df492df5467ca624de08a5333e1c", "version_major": 2, "version_minor": 0 }, "text/plain": [ "A Jupyter Widget" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "0.0 degrees Fahrenheit is -17.77777777777778 degrees Celsius\n", "ERROR: invalid input, value entred must be a float (ie a real number)\n" ] } ], "source": [ "from ipywidgets import widgets\n", "from IPython.display import display\n", "\n", "def f2c(sender):\n", " F = sender.value # Read the text value; note this is a string\n", " try:\n", " F = float(F) # Convert this string into a float\n", " print(F, \"degrees Fahrenheit is \", (F - 32)*(5./9.), \"degrees Celsius\")\n", " except:\n", " print(\"ERROR: invalid input, value entred must be a float (ie a real number)\")\n", "widget_f2c = widgets.Text()\n", "widget_f2c.on_submit(f2c)\n", "display(widget_f2c)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* **Exercise 4.5: Make the program safer**
\n", "Extend the program from the previous exercise to include exception handling such that missing (or invalid) values for *t* and *v0* are detected." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "14468710b70a402fab9ff7e0160ba3c8", "version_major": 2, "version_minor": 0 }, "text/plain": [ "A Jupyter Widget" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from ipywidgets import widgets\n", "from IPython.display import display\n", "\n", "def distance(sender):\n", " try:\n", " t, v0 = sender.value.split(\",\") # Read the text value; note the use of *split*\n", " except:\n", " print(\"ERROR: expecting two values seperated by a ','\")\n", " return\n", " \n", " try:\n", " t = float(t)\n", " v0 = float(v0)\n", " except:\n", " print(\"ERROR: t and v0 must be both float's (ie real numbers)\")\n", " return\n", " \n", " g = 9.81 # Assigns g value\n", " y = v0*t - 0.5*g*t**2 # Computes y\n", " print(\"distance traveled: \", y, \"meters\")\n", "\n", "widget_distance = widgets.Text()\n", "widget_distance.on_submit(distance)\n", "display(widget_distance)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* **Exercise 4.6: Test more in the program**
\n", "Test if the *t* value read in the program from the previous exercise lies between $0$ and ${2v_0}/{g}$. If not, 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." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "6853dfbf6d46489e9bdc1938688794ee", "version_major": 2, "version_minor": 0 }, "text/plain": [ "A Jupyter Widget" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from ipywidgets import widgets\n", "from IPython.display import display\n", "\n", "def distance(sender):\n", " g = 9.81 # Assigns g value\n", "\n", " try:\n", " t, v0 = sender.value.split(\",\") # Read the text value; note the use of *split*\n", " except:\n", " print(\"ERROR: expecting two values seperated by a ','\")\n", " return\n", " \n", " try:\n", " t = float(t)\n", " v0 = float(v0)\n", " except:\n", " print(\"ERROR: t and v0 must be both float's (ie real numbers)\")\n", " return\n", " \n", " try:\n", " if t<0 or t> 2*v0/g:\n", " raise ValueError\n", " except ValueError:\n", " print(\"ERROR: value of t must be between 0 and \", 2*v0/g)\n", " return\n", " \n", " y = v0*t - 0.5*g*t**2 # Computes y\n", " print(\"distance traveled: \", y, \"meters\")\n", "\n", "widget_distance = widgets.Text()\n", "widget_distance.on_submit(distance)\n", "display(widget_distance)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* **Exercise 4.7: Compute the distance it takes to stop a car**
\n", "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
\n", "$d = 0.5\\frac{v_0^2}{\\mu g}$
\n", "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", "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!)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "c066db4a33354184a56c3afee4511a88", "version_major": 2, "version_minor": 0 }, "text/plain": [ "A Jupyter Widget" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from ipywidgets import widgets\n", "from IPython.display import display\n", "\n", "def distance(sender):\n", " g = 9.81 # Assigns g value\n", "\n", " try:\n", " v0, mu = sender.value.split(\",\") # Read the text value; note the use of *split*\n", " except:\n", " print(\"ERROR: expecting two values seperated by a ','\")\n", " return\n", " \n", " try:\n", " v0 = float(v0)\n", " v0 = (v0*1000)/3600 # Conversion from km/h to m/s\n", " mu = float(mu)\n", " except:\n", " print(\"ERROR: v0 and mu must be both float's (ie real numbers)\")\n", " return\n", " \n", " d = (0.5*v0**2)/(mu*g) # Computes braking distance\n", "\n", " print(\"breaking distance: \", d, \"meters\")\n", "\n", "widget_distance = widgets.Text()\n", "widget_distance.on_submit(distance)\n", "display(widget_distance)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.5.2" } }, "nbformat": 4, "nbformat_minor": 1 }