{ "cells": [ { "cell_type": "markdown", "metadata": { "geopyter": { "Contributors": "James Millington (james.millington@kcl.ac.uk)", "git": { "active_branch": "master", "author.name": "Jon Reades", "authored_date": "2017-08-17 19:06:58", "committed_date": "2017-08-17 19:06:58", "committer.name": "Jon Reades", "sha": "5e3b396ae18a982d693c4bfd86c721c7e1e21051" } } }, "source": [ "
\n", "

Recap 1

\n", "
" ] }, { "cell_type": "markdown", "metadata": { "geopyter": { "Contributors": "James Millington (james.millington@kcl.ac.uk)", "git": { "active_branch": "master", "author.name": "Jon Reades", "authored_date": "2017-08-17 19:06:58", "committed_date": "2017-08-17 19:06:58", "committer.name": "Jon Reades", "sha": "5e3b396ae18a982d693c4bfd86c721c7e1e21051" } }, "nbpresent": { "id": "cada64b6-f8c9-4dbb-9ab1-b5617d0cc16a" } }, "source": [ "## First Checkpoint\n", "\n", "After a whirlwind introduction to Python you've made it to the first checkpoint. This isn't about us checking up on you, but rather about you having a chance to check your _own_ understanding of the foundational concepts covered so far. It is really _easy_ to click through the coding cells, become a bit flummoxed, look at the answer quickly 'just to check', and then come away with the impression that you've _understood_ what you are doing: \"Yeah, yeah, I made a little mistake there but it's fine because I was close enough...\"\n", "\n", "We _know_ that's the case because we've _done_ it before you. \n", "\n", "The challenge with coding is that, as the code becomes more complex, so do the issues. And if you haven't learned to be careful with the basics and to _fix them yourself_ then fixing the more subtle 'mistakes' will be _that much harder_. And you don't want this to be any harder than it alreay is!" ] }, { "cell_type": "markdown", "metadata": { "geopyter": { "Contributors": "James Millington (james.millington@kcl.ac.uk)", "git": { "active_branch": "master", "author.name": "Jon Reades", "authored_date": "2017-08-17 19:06:58", "committed_date": "2017-08-17 19:06:58", "committer.name": "Jon Reades", "sha": "5e3b396ae18a982d693c4bfd86c721c7e1e21051" } } }, "source": [ "### Remember: You _Will_ Make Mistakes\n", "\n", "Everyone makes mistakes. We do it all the time because, in programming, it is very easy to make errors. Computers are stupid and do _exactly_ what we tell them, so even tiny mistakes (`aVariable` vs `avariable`, for example) can cause a piece of code or a program to fail to run. Errors in a program are often called bugs. \n", "\n", "Fun fact: the [origin of the term bug](https://en.wikipedia.org/wiki/Software_bug#:~:text=The%20term%20%22bug%22%20was%20used,in%20an%20early%20electromechanical%20computer.&text=Operators%20traced%20an%20error%20in,taped%20to%20the%20log%20book.) was an _actual_ bug (a moth) caught up in the mechanical relay of an early computer.\n", "\n", "Bugs today are rarely of the insectile variety, and we now distinguish between three general types of error: \n", "1. **Syntax errors** are when the ‘grammar’ rules of Python (known as the syntax) have been violated, making it impossible for the code to be parsed (i.e. understood by Python); \n", "2. **Logic errors** have good syntax but there is an error in the order of statements or how they relate to one another;\n", "3. **Semantic errors** make programs do something other than what the programmer intended.\n", "Actually, not all programmers would distinguish between \\#2 and \\#3, but we'll make a case for these being different beasts below.\n", "\n", "Here's a nice little video on this topic...\n", "\n", "[![Video about Types of Errors](https://img.youtube.com/vi/ToPP5UGgJUM/0.jpg)](https://www.youtube.com/watch?v=ToPP5UGgJUM)\n", "\n", "When you first learn to code, syntax errors are the most common type of error because you are still learning the 'rules' of the language: indentation, the difference between `=` and `==`, the need for a colon (`:`) on the end of some types of code... Python is _fairly_ good at telling you what has caused a syntax error when it's trivial and happens on a single line of code, but it will get quite a bit harder as your code becomes more complex!\n", "\n", "The other types of errors are ones that you learn to spot over time through experience and developing skills. Learning to spot logic errors is one of those wonderful transferrable skills that you can _also_ use in everyday life: by learning to think through challenges at a higher level and then address them iteratively you are likely to become both a more careful thinker (i.e. _how_ should I go about solving this issue?), and a more flexible and resilient thinker (i.e. ok, so _that_ didn't work out as I expected, what should do I do now?). \n", "\n", "> _“A good example of a logic error might be, 'take a drink from your water bottle, put it in your backpack, walk to the library, and then put the top back on the bottle.'”_ Severance (2014, p.14)\n", "\n", "Many sites don't even discuss the concept of a semantic error or treat it as the equivalent of a logic error, but we think it's useful to set a bar _beyond_ 'basic' logic errors: these arise from _interactions_ between parts of a program, or between a programming language and the underlying computer/operating system. So the code can (and usually does) run successfully but the output is a long way from what you expected, or the failures that you're seeing _don't make sense_. We'd suggest that spotting semantic errors is a sign that you are well on your way to _thinking_ computationally. In short, you understand why your code is triggering strange and unexpected behaviour even though it _looks_ like it should do exactly what you intended. " ] }, { "cell_type": "markdown", "metadata": { "geopyter": { "Contributors": "James Millington (james.millington@kcl.ac.uk)", "git": { "active_branch": "master", "author.name": "Jon Reades", "authored_date": "2017-08-17 19:06:58", "committed_date": "2017-08-17 19:06:58", "committer.name": "Jon Reades", "sha": "5e3b396ae18a982d693c4bfd86c721c7e1e21051" } } }, "source": [ "## Exercises" ] }, { "cell_type": "markdown", "metadata": { "geopyter": { "Contributors": "James Millington (james.millington@kcl.ac.uk)", "git": { "active_branch": "master", "author.name": "Jon Reades", "authored_date": "2017-08-17 19:06:58", "committed_date": "2017-08-17 19:06:58", "committer.name": "Jon Reades", "sha": "5e3b396ae18a982d693c4bfd86c721c7e1e21051" } } }, "source": [ "#### 1.1 Variables and Assignment\n", "\n", "Without running the code below, calculate what the values of `x`, `y` and `z` would be after all lines of code in the code block have been executed." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "geopyter": { "Contributors": "James Millington (james.millington@kcl.ac.uk)", "git": { "active_branch": "master", "author.name": "Jon Reades", "authored_date": "2017-08-17 19:06:58", "committed_date": "2017-08-17 19:06:58", "committer.name": "Jon Reades", "sha": "5e3b396ae18a982d693c4bfd86c721c7e1e21051" } } }, "outputs": [], "source": [ "x = 12\n", "y = 10\n", "\n", "z = x + y\n", "x = x + y\n", "y = z + y" ] }, { "cell_type": "markdown", "metadata": { "geopyter": { "Contributors": "James Millington (james.millington@kcl.ac.uk)", "git": { "active_branch": "master", "author.name": "Jon Reades", "authored_date": "2017-08-17 19:06:58", "committed_date": "2017-08-17 19:06:58", "committer.name": "Jon Reades", "sha": "5e3b396ae18a982d693c4bfd86c721c7e1e21051" } } }, "source": [ "Once you have calculated what you think `x`, `y` and `z` are, add print statements to the code above and run it to check. Make sure you understand the results you find. " ] }, { "cell_type": "markdown", "metadata": { "geopyter": { "Contributors": "James Millington (james.millington@kcl.ac.uk)", "git": { "active_branch": "master", "author.name": "Jon Reades", "authored_date": "2017-08-17 19:06:58", "committed_date": "2017-08-17 19:06:58", "committer.name": "Jon Reades", "sha": "5e3b396ae18a982d693c4bfd86c721c7e1e21051" } } }, "source": [ "#### 1.2 Operator Precedence\n", "\n", "Without running the code below, calculate what the values of `x`, `y` and `z` would be after all lines of code in the code block have been executed." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "geopyter": { "Contributors": "James Millington (james.millington@kcl.ac.uk)", "git": { "active_branch": "master", "author.name": "Jon Reades", "authored_date": "2017-08-17 19:06:58", "committed_date": "2017-08-17 19:06:58", "committer.name": "Jon Reades", "sha": "5e3b396ae18a982d693c4bfd86c721c7e1e21051" } } }, "outputs": [], "source": [ "x = 1 + 2 * 3 / 4\n", "y = (1 + 2) * (3 / 4)\n", "z = 1 + 2 * (3 / 4)\n", "\n", "print(x)\n", "print(y)\n", "print(z)" ] }, { "cell_type": "markdown", "metadata": { "geopyter": { "Contributors": "James Millington (james.millington@kcl.ac.uk)", "git": { "active_branch": "master", "author.name": "Jon Reades", "authored_date": "2017-08-17 19:06:58", "committed_date": "2017-08-17 19:06:58", "committer.name": "Jon Reades", "sha": "5e3b396ae18a982d693c4bfd86c721c7e1e21051" } } }, "source": [ "Once you have calculated what you think `x`, `y` and `z` are, add print statements to the code above and run it to check." ] }, { "cell_type": "markdown", "metadata": { "geopyter": { "Contributors": "James Millington (james.millington@kcl.ac.uk)", "git": { "active_branch": "master", "author.name": "Jon Reades", "authored_date": "2017-08-17 19:06:58", "committed_date": "2017-08-17 19:06:58", "committer.name": "Jon Reades", "sha": "5e3b396ae18a982d693c4bfd86c721c7e1e21051" } } }, "source": [ "#### 1.3 Thinking Abstractly\n", "\n", "*Hint*: to solve the following problems assume `pi = 3.141592653589793`\n", "\n", "Given a sphere of diameter 12cm, calculate its volume (use Google to find the formula for this):" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "geopyter": { "Contributors": "James Millington (james.millington@kcl.ac.uk)", "git": { "active_branch": "master", "author.name": "Jon Reades", "authored_date": "2017-08-17 19:06:58", "committed_date": "2017-08-17 19:06:58", "committer.name": "Jon Reades", "sha": "5e3b396ae18a982d693c4bfd86c721c7e1e21051" } }, "solution2": "hidden", "solution2_first": true }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "solution2": "hidden" }, "outputs": [], "source": [ "pi = 3.141592653589793\n", "r = 12 / 2\n", "vol = (4/3) * pi * (r**3)\n", "print(vol)" ] }, { "cell_type": "markdown", "metadata": { "geopyter": { "Contributors": "James Millington (james.millington@kcl.ac.uk)", "git": { "active_branch": "master", "author.name": "Jon Reades", "authored_date": "2017-08-17 19:06:58", "committed_date": "2017-08-17 19:06:58", "committer.name": "Jon Reades", "sha": "5e3b396ae18a982d693c4bfd86c721c7e1e21051" } } }, "source": [ "Given a sphere of volume 14,137cm3 calculate its radius to the closest **whole number** (this will involve rearranging the formula and you may need to good how to find a cube root with python):" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "solution2": "hidden", "solution2_first": true }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "solution2": "hidden" }, "outputs": [], "source": [ "vol=14137\n", "r3 = vol / ((4/3) * pi)\n", "r = r3**(1/3)\n", "print(round(r))" ] }, { "cell_type": "markdown", "metadata": { "geopyter": { "Contributors": "James Millington (james.millington@kcl.ac.uk)", "git": { "active_branch": "master", "author.name": "Jon Reades", "authored_date": "2017-08-17 19:06:58", "committed_date": "2017-08-17 19:06:58", "committer.name": "Jon Reades", "sha": "5e3b396ae18a982d693c4bfd86c721c7e1e21051" } } }, "source": [ "#### 2.1 Working with Conditions\n", "\n", "**This is not a loop question, we just want you to change the value of _hours_ so that for each of the cases below (_hours_ is set to 10, 2, 0 in turn) you get the right output.**\n", "\n", "Using the code below as a starting point, add two lines of code so that your output is as follows:\n", "1. Code prints `Hours are greater than zero` when `hours=10`\n", "2. Code prints `Hours are greater than zero` _and_ `Hours are less than 10` when `hours=2` \n", "3. Code prints `Hours are less than 10` when `hours=0`" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "solution2": "hidden", "solution2_first": true }, "outputs": [], "source": [ "hours=10\n", "\n", "if hours > 0:\n", " print(\" \")" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "solution2": "hidden" }, "outputs": [], "source": [ "hours=2\n", "\n", "if hours > 0:\n", " print(\"Hours are greater than zero\")\n", "if hours < 10:\n", " print(\"Hours are less than 10\")" ] }, { "cell_type": "markdown", "metadata": { "geopyter": { "Contributors": "James Millington (james.millington@kcl.ac.uk)", "git": { "active_branch": "master", "author.name": "Jon Reades", "authored_date": "2017-08-17 19:06:58", "committed_date": "2017-08-17 19:06:58", "committer.name": "Jon Reades", "sha": "5e3b396ae18a982d693c4bfd86c721c7e1e21051" } } }, "source": [ "#### 2.2 Flow Control\n", "\n", "In the code cell below, enter code to execute the flow chart shown in the image below. You will need to assign values to `a` and `b` on lines 1 and 2 before using the flow chart to complete the code such that it produces the following:\n", "1. When `a = 2` and `b = 2` four lines of output will be written\n", "2. When `a = 1` and `b = 2` one line of output will be written\n", "\n", "" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "solution2": "hidden", "solution2_first": true }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "solution2": "hidden" }, "outputs": [], "source": [ "a = 1\n", "b = 2\n", "\n", "x = a\n", "if x == b:\n", " print(\"inside 1\")\n", " print(\"inside 2\")\n", " print(\"inside 3\")\n", "print(\"after condition\")" ] }, { "cell_type": "markdown", "metadata": { "geopyter": { "Contributors": "James Millington (james.millington@kcl.ac.uk)", "git": { "active_branch": "master", "author.name": "Jon Reades", "authored_date": "2017-08-17 19:06:58", "committed_date": "2017-08-17 19:06:58", "committer.name": "Jon Reades", "sha": "5e3b396ae18a982d693c4bfd86c721c7e1e21051" } } }, "source": [ "#### 3.1 It's All Quite Logical\n", "\n", "Read the code in the code cell below. " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "geopyter": { "Contributors": "James Millington (james.millington@kcl.ac.uk)", "git": { "active_branch": "master", "author.name": "Jon Reades", "authored_date": "2017-08-17 19:06:58", "committed_date": "2017-08-17 19:06:58", "committer.name": "Jon Reades", "sha": "5e3b396ae18a982d693c4bfd86c721c7e1e21051" } } }, "outputs": [], "source": [ "x = 1\n", "if x > 0 and x < 5:\n", " print(\"Joe\")\n", " \n", "if x > 0 or x < 5:\n", " print(\"Aled\")\n", " \n", "if not(x > 0):\n", " print(\"Sarah\")" ] }, { "cell_type": "markdown", "metadata": { "geopyter": { "Contributors": "James Millington (james.millington@kcl.ac.uk)", "git": { "active_branch": "master", "author.name": "Jon Reades", "authored_date": "2017-08-17 19:06:58", "committed_date": "2017-08-17 19:06:58", "committer.name": "Jon Reades", "sha": "5e3b396ae18a982d693c4bfd86c721c7e1e21051" } } }, "source": [ "Assume that the code executes for a given value of `x` and answer the following questions. Only test the code **AFTER** you have worked out the answers and typed them below.\n", "\n", "1.\tWhat names are name(s) are printed when `x = 5`? \n", "2.\tWhat value(s) can `x` be when the names `Joe` and `Aled` are printed? \n", "3.\tWhat name(s) are printed when `x = -1`?\n", "4.\tIs there any value for which all three names will be printed?\n" ] }, { "cell_type": "markdown", "metadata": { "geopyter": { "Contributors": "James Millington (james.millington@kcl.ac.uk)", "git": { "active_branch": "master", "author.name": "Jon Reades", "authored_date": "2017-08-17 19:06:58", "committed_date": "2017-08-17 19:06:58", "committer.name": "Jon Reades", "sha": "5e3b396ae18a982d693c4bfd86c721c7e1e21051" } }, "solution2": "hidden", "solution2_first": true }, "source": [ "**Answers** _Edit this cell!_\n", "\n", "To edit this cell just double click on it (right _here_ ). When you're finished, click the _run cell_ button in the toolbar just below the menu near the top of the window.\n", "\n", "1.\n", "\n", "2.\n", "\n", "3.\n", "\n", "4." ] }, { "cell_type": "markdown", "metadata": { "solution2": "hidden" }, "source": [ "1. Aled\n", "\n", "2. 1, 2, 3, 4\n", "\n", "3. Aled, Sarah\n", "\n", "4. No" ] }, { "cell_type": "markdown", "metadata": { "geopyter": { "Contributors": "James Millington (james.millington@kcl.ac.uk)", "git": { "active_branch": "master", "author.name": "Jon Reades", "authored_date": "2017-08-17 19:06:58", "committed_date": "2017-08-17 19:06:58", "committer.name": "Jon Reades", "sha": "5e3b396ae18a982d693c4bfd86c721c7e1e21051" } } }, "source": [ "#### 3.2 Logic (Cont'd)\n", "\n", "Study the flow chart below.\n", "\n", "\n", "\n", "In the cell below, use the for loop already set up to execute the alternative execution shown by the flow chart for values of `x` between 0 and 9. Check your code works. Also check you understand why the Modulo `%` allows us to find odd vs even numbers. " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "solution2": "hidden", "solution2_first": true }, "outputs": [], "source": [ "for x in range(0,9):\n", " # ... do something ..." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "solution2": "hidden" }, "outputs": [], "source": [ "for x in range(0,9):\n", " print(x)\n", " if(x%2 == 0):\n", " print(\"x is even\")\n", " else:\n", " print(\"x is odd\")\n", " print(x)" ] }, { "cell_type": "markdown", "metadata": { "geopyter": { "Contributors": "James Millington (james.millington@kcl.ac.uk)", "git": { "active_branch": "master", "author.name": "Jon Reades", "authored_date": "2017-08-17 19:06:58", "committed_date": "2017-08-17 19:06:58", "committer.name": "Jon Reades", "sha": "5e3b396ae18a982d693c4bfd86c721c7e1e21051" } } }, "source": [ "#### 3.3 Nested Conditions\n", "\n", "Conditional statements can be nested within one another. That is, once one conditional statement has been evaluated others may subsequently be evaluated (or not) depending on the result of the initial conditional statement. The code below shows an example of this. " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "geopyter": { "Contributors": "James Millington (james.millington@kcl.ac.uk)", "git": { "active_branch": "master", "author.name": "Jon Reades", "authored_date": "2017-08-17 19:06:58", "committed_date": "2017-08-17 19:06:58", "committer.name": "Jon Reades", "sha": "5e3b396ae18a982d693c4bfd86c721c7e1e21051" } } }, "outputs": [], "source": [ "if x != y: #line 1\n", " print(\"x is not equal to y\")\n", " \n", " if(x > y): #line 4\n", " print(\"x is greater than y\")\n", " \n", " else: #line 7\n", " print(\"x is less than y\")\n", "\n", "else:\n", " print(\"insert conclusion here\")" ] }, { "cell_type": "markdown", "metadata": { "geopyter": { "Contributors": "James Millington (james.millington@kcl.ac.uk)", "git": { "active_branch": "master", "author.name": "Jon Reades", "authored_date": "2017-08-17 19:06:58", "committed_date": "2017-08-17 19:06:58", "committer.name": "Jon Reades", "sha": "5e3b396ae18a982d693c4bfd86c721c7e1e21051" } } }, "source": [ "Note how the indentation makes it easier to work out which ‘level’ the code is operating on. In the code above, lines 4 and 7 are at the same indentation meaning that both will be skipped if the initial condition (on line 1) is `False`. \n", "\n", "To check you understand how the code above works:\n", "1. Change `insert conclusion here` in the code above to a string that helps to explain the condition of `x` and `y`\n", "2. **Before running the code** for `x = 2` and `y = 3`, type below what line(s) will be output " ] }, { "cell_type": "markdown", "metadata": { "solution2": "hidden", "solution2_first": true }, "source": [ "Output for `x = 2` and `y = 3` is:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "solution2": "hidden" }, "outputs": [], "source": [ "x = 2\n", "y = 3\n", "if x != y: #line 1\n", " print(\"x is not equal to y\")\n", " \n", " if(x > y): #line 4\n", " print(\"x is greater than y\")\n", " \n", " else: #line 7\n", " print(\"x is less than y\")\n", "\n", "else:\n", " print(\"x is equal to y\")" ] }, { "cell_type": "markdown", "metadata": { "geopyter": { "Contributors": "James Millington (james.millington@kcl.ac.uk)", "git": { "active_branch": "master", "author.name": "Jon Reades", "authored_date": "2017-08-17 19:06:58", "committed_date": "2017-08-17 19:06:58", "committer.name": "Jon Reades", "sha": "5e3b396ae18a982d693c4bfd86c721c7e1e21051" } } }, "source": [ "#### 4.1. Alterative Execution\n", "In the code cell below, enter code that reports whether person A is older than person B or not. Whole numbers should be used as ages in years and these ages should be specified on the first two lines of code. Either “Person A is older than Person B” or “Person A is not older than Person B” should be reported to the user." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "solution2": "hidden", "solution2_first": true }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "solution2": "hidden" }, "outputs": [], "source": [ "ageA = 20\n", "ageB = 19\n", "\n", "if ageA > ageB:\n", " print(\"Person A is older than Person B\")\n", "else:\n", " print(\"Person A is not older than Person B\")" ] }, { "cell_type": "markdown", "metadata": { "geopyter": { "Contributors": "James Millington (james.millington@kcl.ac.uk)", "git": { "active_branch": "master", "author.name": "Jon Reades", "authored_date": "2017-08-17 19:06:58", "committed_date": "2017-08-17 19:06:58", "committer.name": "Jon Reades", "sha": "5e3b396ae18a982d693c4bfd86c721c7e1e21051" } } }, "source": [ "#### 4.2. Conditional Execution\n", "Copy your final code from the previous exercise into the code block below. Now build on this code to check if Person A is older than Person B, but also whether they are the same age. One of following three responses should be reported to the user:\n", "1.\t“Person A is older than Person B”\n", "2.\t“Person B is older than Person A”\n", "3.\t“Person A and Person B are the same age”" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "solution2": "hidden", "solution2_first": true }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "solution2": "hidden" }, "outputs": [], "source": [ "ageA = 20\n", "ageB = 20\n", "\n", "if ageA > ageB:\n", " print(\"Person A is older than Person B\")\n", "elif ageA < ageB:\n", " print(\"Person B is older than Person A\")\n", "else:\n", " print(\"Person A and Person B are the same age\")" ] }, { "cell_type": "markdown", "metadata": { "geopyter": { "Contributors": "James Millington (james.millington@kcl.ac.uk)", "git": { "active_branch": "master", "author.name": "Jon Reades", "authored_date": "2017-08-17 19:06:58", "committed_date": "2017-08-17 19:06:58", "committer.name": "Jon Reades", "sha": "5e3b396ae18a982d693c4bfd86c721c7e1e21051" } } }, "source": [ "#### 4.3. Nested Execution\n", "Copy your final code from the previous exercise into the code block below. Build on this code with a nested execution structure to report if Person A is much younger, a little younger, the same age, a little older or much older than Person B. Write your code with a nested structure so that first it evaluates whether one person is older than the other, and then, if so, whether there is a large difference in age or a small difference in age. You should create a variable to specify what the number of years is that indicates a ‘large difference’ in age. One of the following five responses should be reported to the user:\n", "1.\t“Person A is much younger than Person B” [if age difference > ‘large difference’]\n", "2.\t“Person A is a little younger than Person B” [if age difference <= ‘large difference’]\n", "3.\t“Person A is the same age as Person B”\n", "4.\t“Person A is a little older than Person B” [if age difference <= ‘large difference’]\n", "5.\t“Person A is a much older than Person B” [if age difference > ‘large difference’]\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "geopyter": { "Contributors": "James Millington (james.millington@kcl.ac.uk)", "git": { "active_branch": "master", "author.name": "Jon Reades", "authored_date": "2017-08-17 19:06:58", "committed_date": "2017-08-17 19:06:58", "committer.name": "Jon Reades", "sha": "5e3b396ae18a982d693c4bfd86c721c7e1e21051" } }, "solution2": "hidden", "solution2_first": true }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "solution2": "hidden" }, "outputs": [], "source": [ "ageA = 25\n", "ageB = 30\n", "largeD = 20\n", "\n", "diff = ageA - ageB\n", "\n", "if diff <= (largeD * -1):\n", " print(\"Person A is much younger than Person B\")\n", "elif diff < 0 and diff > (largeD * -1):\n", " print(\"Person A is a little younger than Person B\")\n", "elif diff == 0:\n", " print(\"Person A is the same age as Person B\")\n", "elif diff >= (largeD * 1):\n", " print(\"Person A is much older than Person B\")\n", "elif diff > 0 and diff < (largeD * 1): \n", " print(\"Person A is a little older than Person B\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 4.4a Bonus! Managing Errors\n", "\n", "Python provides tools to 'handle exceptions' (i.e. manage errors). [For example](read more at https://docs.python.org/3/tutorial/errors.html#handling-exceptions) you can use combinations of `try`, `except`, `else` and `finally` keywords. \n", "\n", "Read the link above (and maybe Google yourself for some other examples) and use the information gained to handle an exception in which the user an age as a `string` rather than a integer (e.g. print \"Invalid Input\" if `diff` cannot be calculated):" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "solution2": "hidden", "solution2_first": true }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "solution2": "hidden" }, "outputs": [], "source": [ "ageA = \"five\"\n", "ageB = 30\n", "largeD = 20\n", "\n", "try:\n", " diff = ageA - ageB\n", "except:\n", " print(\"Invalid Input\")\n", "else: \n", " if diff <= (largeD * -1):\n", " print(\"Person A is much younger than Person B\")\n", " elif diff < 0 and diff > (largeD * -1):\n", " print(\"Person A is a little younger than Person B\")\n", " elif diff == 0:\n", " print(\"Person A is the same age as Person B\")\n", " elif diff >= (largeD * 1):\n", " print(\"Person A is much older than Person B\")\n", " elif diff > 0 and diff < (largeD * 1): \n", " print(\"Person A is a little older than Person B\")\n", " \n", "#read more at https://docs.python.org/3/tutorial/errors.html#handling-exceptions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### 4.4b Bonus! Managing Errors\n", "\n", "Another way to trap errors is by 'raising exceptions'. [This requires](https://docs.python.org/3/tutorial/errors.html#raising-exceptions) using the `raise` keyword. \n", "\n", "Read the link above (and maybe Google yourself for some other examples) and use the information gained to raise an exception if the user provide a negative age (outputing \"Invalid Age\" in the error mmessage):" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "solution2": "hidden", "solution2_first": true }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "solution2": "hidden" }, "outputs": [], "source": [ "ageA = -1\n", "ageB = 30\n", "largeD = 20\n", "\n", "diff = ageA - ageB\n", "\n", "if ageA < 0 or ageB < 0:\n", " raise Exception(\"Invalid Age\") \n", "else: \n", " if diff <= (largeD * -1):\n", " print(\"Person A is much younger than Person B\")\n", " elif diff < 0 and diff > (largeD * -1):\n", " print(\"Person A is a little younger than Person B\")\n", " elif diff == 0:\n", " print(\"Person A is the same age as Person B\")\n", " elif diff >= (largeD * 1):\n", " print(\"Person A is much older than Person B\")\n", " elif diff > 0 and diff < (largeD * 1): \n", " print(\"Person A is a little older than Person B\")\n", " \n", "#read more at https://docs.python.org/3/tutorial/errors.html#raising-exceptions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Credits!\n", "\n", "#### Contributors:\n", "The following individuals have contributed to these teaching materials: \n", "- [James Millington](https://github.com/jamesdamillington)\n", "- [Jon Reades](https://github.com/jreades)\n", "- [Michele Ferretti](https://github.com/miccferr)\n", "- [Zahratu Shabrina](https://github.com/zarashabrina)\n", "\n", "#### License\n", "The content and structure of this teaching project itself is licensed under the [Creative Commons Attribution-NonCommercial-ShareAlike 4.0 license](https://creativecommons.org/licenses/by-nc-sa/4.0/), and the contributing source code is licensed under [The MIT License](https://opensource.org/licenses/mit-license.php).\n", "\n", "#### Acknowledgements:\n", "Supported by the [Royal Geographical Society](https://www.rgs.org/HomePage.htm) (with the Institute of British Geographers) with a Ray Y Gildea Jr Award.\n", "\n", "#### Potential Dependencies:\n", "This notebook may depend on the following libraries: None" ] } ], "metadata": { "anaconda-cloud": {}, "geopyter": { "Contributors": [ "James Millington (james.millington@kcl.ac.uk)" ], "git": { "active_branch": "master", "author.name": "Jon Reades", "authored_date": "2017-08-17 19:06:58", "committed_date": "2017-08-17 19:06:58", "committer.name": "Jon Reades", "sha": "5e3b396ae18a982d693c4bfd86c721c7e1e21051" }, "libs": {} }, "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.10.12" } }, "nbformat": 4, "nbformat_minor": 4 }