|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# 30. Free ticket at the museum\n", |
| 8 | + "\n", |
| 9 | + "## Input validation and outputs variations\n", |
| 10 | + "\n", |
| 11 | + "[Learn Python with Jupyter](https://learnpythonwithjupyter.com/) by [Serena Bonaretti](https://sbonaretti.github.io/) \n", |
| 12 | + "Narrative license: [CC BY-NC-SA](https://creativecommons.org/licenses/by-nc-sa/2.0/). Code license: [GNU-GPL v3](https://www.gnu.org/licenses/gpl-3.0.en.html) \n", |
| 13 | + "\n", |
| 14 | + "---" |
| 15 | + ] |
| 16 | + }, |
| 17 | + { |
| 18 | + "cell_type": "markdown", |
| 19 | + "metadata": {}, |
| 20 | + "source": [ |
| 21 | + "- You work at a museum and have to update the online system to buy tickets. The update is that people who are 65 and older now qualify for a free ticket. Write a function that asks visitors to enter their prefix, last name, and age; checks the *types* and *values* of these inputs; and returns a message telling the visitor if they are eligible for a free ticket." |
| 22 | + ] |
| 23 | + }, |
| 24 | + { |
| 25 | + "cell_type": "code", |
| 26 | + "execution_count": null, |
| 27 | + "metadata": {}, |
| 28 | + "outputs": [], |
| 29 | + "source": [ |
| 30 | + "def free_museum_ticket(prefix, last_name, age): \n", |
| 31 | + " \"\"\"Returns a message containing inputs and free ticket eligibility based on age\n", |
| 32 | + " E.g. Mrs. Holmes, you are eligible for a free museum ticket because you are 66\n", |
| 33 | + " \n", |
| 34 | + " Parameters\n", |
| 35 | + " ----------\n", |
| 36 | + " prefix : string\n", |
| 37 | + " Ms, Mrs, Mr\n", |
| 38 | + " last_name : string\n", |
| 39 | + " Last name of a visitor\n", |
| 40 | + " age : integer\n", |
| 41 | + " Age of a visitor\n", |
| 42 | + " \n", |
| 43 | + " Returns\n", |
| 44 | + " -------\n", |
| 45 | + " string \n", |
| 46 | + " Message containing visitor inputs and eligibility\n", |
| 47 | + " \"\"\"\n", |
| 48 | + " \n", |
| 49 | + " # --- checking parameter types ---\n", |
| 50 | + " \n", |
| 51 | + " # the type of prefix must be string\n", |
| 52 | + " if not isinstance (prefix, str): \n", |
| 53 | + " raise TypeError (\"prefix must be a string\")\n", |
| 54 | + " \n", |
| 55 | + " # the type of last_name must be string\n", |
| 56 | + " if not isinstance (last_name, str): \n", |
| 57 | + " raise TypeError (\"last_name must be a string\")\n", |
| 58 | + " \n", |
| 59 | + " # the type of age must be integer\n", |
| 60 | + " if not isinstance (age, int): \n", |
| 61 | + " raise TypeError (\"age must be an integer\") \n", |
| 62 | + "\n", |
| 63 | + " \n", |
| 64 | + " # --- checking parameter values ---\n", |
| 65 | + " \n", |
| 66 | + " # prefix must be Ms, Mrs, or Mr\n", |
| 67 | + " if prefix not in [\"Ms\", \"Mrs\", \"Mr\"]:\n", |
| 68 | + " raise ValueError (\"prefix must be Ms, Mrs, or Mr\")\n", |
| 69 | + " \n", |
| 70 | + " # last_name must contain only characters \n", |
| 71 | + " if not last_name.isalpha():\n", |
| 72 | + " raise ValueError (\"last_name must contain only letters\")\n", |
| 73 | + " \n", |
| 74 | + " # age has to be between 0 and 125\n", |
| 75 | + " if age < 0 or age > 125:\n", |
| 76 | + " raise ValueError (\"age must be between 0 and 125\")\n", |
| 77 | + "\n", |
| 78 | + " \n", |
| 79 | + " # --- returning output ---\n", |
| 80 | + "\n", |
| 81 | + " if age >= 65:\n", |
| 82 | + " return prefix + \". \" + last_name + \", you are eligible for a free museum ticket because you are \" + str(age)\n", |
| 83 | + " else:\n", |
| 84 | + " return prefix + \". \" + last_name + \", you are not eligible for a free museum ticket because you are \" + str(age) " |
| 85 | + ] |
| 86 | + }, |
| 87 | + { |
| 88 | + "cell_type": "markdown", |
| 89 | + "metadata": {}, |
| 90 | + "source": [ |
| 91 | + "---\n", |
| 92 | + "- Call the function checking the parameter *types*:" |
| 93 | + ] |
| 94 | + }, |
| 95 | + { |
| 96 | + "cell_type": "code", |
| 97 | + "execution_count": null, |
| 98 | + "metadata": {}, |
| 99 | + "outputs": [], |
| 100 | + "source": [ |
| 101 | + "# checking prefix type\n", |
| 102 | + "message = free_museum_ticket(1, \"Holmes\", 66)\n", |
| 103 | + "print (message)" |
| 104 | + ] |
| 105 | + }, |
| 106 | + { |
| 107 | + "cell_type": "code", |
| 108 | + "execution_count": null, |
| 109 | + "metadata": {}, |
| 110 | + "outputs": [], |
| 111 | + "source": [ |
| 112 | + "# checking last_name type\n", |
| 113 | + "message = free_museum_ticket(\"Mrs\", 1.2, 66)\n", |
| 114 | + "print (message)" |
| 115 | + ] |
| 116 | + }, |
| 117 | + { |
| 118 | + "cell_type": "code", |
| 119 | + "execution_count": null, |
| 120 | + "metadata": {}, |
| 121 | + "outputs": [], |
| 122 | + "source": [ |
| 123 | + "# checking age type\n", |
| 124 | + "message = free_museum_ticket(\"Mrs\", \"Holmes\", \"Hi\") \n", |
| 125 | + "print (message)" |
| 126 | + ] |
| 127 | + }, |
| 128 | + { |
| 129 | + "cell_type": "markdown", |
| 130 | + "metadata": {}, |
| 131 | + "source": [ |
| 132 | + "---\n", |
| 133 | + "- Call the function checking parameter *values*:" |
| 134 | + ] |
| 135 | + }, |
| 136 | + { |
| 137 | + "cell_type": "code", |
| 138 | + "execution_count": null, |
| 139 | + "metadata": {}, |
| 140 | + "outputs": [], |
| 141 | + "source": [ |
| 142 | + "# checking prefix value\n", |
| 143 | + "message = free_museum_ticket(\"Dr\", \"Holmes\", 66)\n", |
| 144 | + "print (message)" |
| 145 | + ] |
| 146 | + }, |
| 147 | + { |
| 148 | + "cell_type": "code", |
| 149 | + "execution_count": null, |
| 150 | + "metadata": {}, |
| 151 | + "outputs": [], |
| 152 | + "source": [ |
| 153 | + "# checking last_name value\n", |
| 154 | + "message = free_museum_ticket(\"Mrs\", \"82\", 66)\n", |
| 155 | + "print (message)" |
| 156 | + ] |
| 157 | + }, |
| 158 | + { |
| 159 | + "cell_type": "code", |
| 160 | + "execution_count": null, |
| 161 | + "metadata": {}, |
| 162 | + "outputs": [], |
| 163 | + "source": [ |
| 164 | + "# checking age value\n", |
| 165 | + "message = free_museum_ticket(\"Mrs\", \"Holmes\", 130)\n", |
| 166 | + "print (message)" |
| 167 | + ] |
| 168 | + }, |
| 169 | + { |
| 170 | + "cell_type": "markdown", |
| 171 | + "metadata": {}, |
| 172 | + "source": [ |
| 173 | + "---\n", |
| 174 | + "- Call the function testing the two possible returns:" |
| 175 | + ] |
| 176 | + }, |
| 177 | + { |
| 178 | + "cell_type": "code", |
| 179 | + "execution_count": null, |
| 180 | + "metadata": {}, |
| 181 | + "outputs": [], |
| 182 | + "source": [ |
| 183 | + "# person is eligible\n", |
| 184 | + "message = free_museum_ticket(\"Mrs\", \"Holmes\", 66)\n", |
| 185 | + "print (message)" |
| 186 | + ] |
| 187 | + }, |
| 188 | + { |
| 189 | + "cell_type": "code", |
| 190 | + "execution_count": null, |
| 191 | + "metadata": {}, |
| 192 | + "outputs": [], |
| 193 | + "source": [ |
| 194 | + "# person is not eligible\n", |
| 195 | + "message = free_museum_ticket(\"Mrs\", \"Choi\", 38)\n", |
| 196 | + "print (message)" |
| 197 | + ] |
| 198 | + } |
| 199 | + ], |
| 200 | + "metadata": { |
| 201 | + "kernelspec": { |
| 202 | + "display_name": "Python 3 (ipykernel)", |
| 203 | + "language": "python", |
| 204 | + "name": "python3" |
| 205 | + }, |
| 206 | + "language_info": { |
| 207 | + "codemirror_mode": { |
| 208 | + "name": "ipython", |
| 209 | + "version": 3 |
| 210 | + }, |
| 211 | + "file_extension": ".py", |
| 212 | + "mimetype": "text/x-python", |
| 213 | + "name": "python", |
| 214 | + "nbconvert_exporter": "python", |
| 215 | + "pygments_lexer": "ipython3", |
| 216 | + "version": "3.9.6" |
| 217 | + }, |
| 218 | + "widgets": { |
| 219 | + "application/vnd.jupyter.widget-state+json": { |
| 220 | + "state": {}, |
| 221 | + "version_major": 2, |
| 222 | + "version_minor": 0 |
| 223 | + } |
| 224 | + } |
| 225 | + }, |
| 226 | + "nbformat": 4, |
| 227 | + "nbformat_minor": 4 |
| 228 | +} |
0 commit comments