|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "id": "a7ada90d-95d5-4241-8d0c-866a667b953a", |
| 6 | + "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "# Specific Errors\n", |
| 9 | + "## ZeroDivisionError occurred: integer division or modulo by zero" |
| 10 | + ] |
| 11 | + }, |
| 12 | + { |
| 13 | + "cell_type": "code", |
| 14 | + "execution_count": 1, |
| 15 | + "id": "7351890a-92b6-4ab3-bf7a-bc6539d13399", |
| 16 | + "metadata": {}, |
| 17 | + "outputs": [ |
| 18 | + { |
| 19 | + "name": "stdout", |
| 20 | + "output_type": "stream", |
| 21 | + "text": [ |
| 22 | + "How many cookies are you baking? 8\n", |
| 23 | + "How many people are attending? 3\n" |
| 24 | + ] |
| 25 | + }, |
| 26 | + { |
| 27 | + "name": "stdout", |
| 28 | + "output_type": "stream", |
| 29 | + "text": [ |
| 30 | + "\n", |
| 31 | + "Let's party! We'll have 3 people attending, they'll each get to eat 2 cookies, and we'll have 2 left over.\n" |
| 32 | + ] |
| 33 | + }, |
| 34 | + { |
| 35 | + "name": "stdout", |
| 36 | + "output_type": "stream", |
| 37 | + "text": [ |
| 38 | + "\n", |
| 39 | + "Would you like to party more? (y or n) y\n", |
| 40 | + "How many cookies are you baking? 8\n", |
| 41 | + "How many people are attending? 0\n" |
| 42 | + ] |
| 43 | + }, |
| 44 | + { |
| 45 | + "name": "stdout", |
| 46 | + "output_type": "stream", |
| 47 | + "text": [ |
| 48 | + "ZeroDivisionError occurred: integer division or modulo by zero\n" |
| 49 | + ] |
| 50 | + }, |
| 51 | + { |
| 52 | + "name": "stdout", |
| 53 | + "output_type": "stream", |
| 54 | + "text": [ |
| 55 | + "\n", |
| 56 | + "Would you like to party more? (y or n) n\n" |
| 57 | + ] |
| 58 | + } |
| 59 | + ], |
| 60 | + "source": [ |
| 61 | + "# Define a function called \"party_planner\" to calculate cookie distribution\n", |
| 62 | + "def party_planner(cookies, people):\n", |
| 63 | + " # Initialize variables for leftovers and cookies per person\n", |
| 64 | + " leftovers = None\n", |
| 65 | + " num_each = None\n", |
| 66 | + "\n", |
| 67 | + " try:\n", |
| 68 | + " # Calculate the number of cookies each person will get\n", |
| 69 | + " num_each = cookies // people\n", |
| 70 | + " # Calculate the remaining cookies (leftovers)\n", |
| 71 | + " leftovers = cookies % people\n", |
| 72 | + " except ZeroDivisionError as e:\n", |
| 73 | + " # Handle the case when the number of people is zero\n", |
| 74 | + " print(\"ZeroDivisionError occurred: {}\".format(e))\n", |
| 75 | + "\n", |
| 76 | + " # Return the results\n", |
| 77 | + " return num_each, leftovers\n", |
| 78 | + "\n", |
| 79 | + "# Initialize a flag for party continuation\n", |
| 80 | + "lets_party = 'y'\n", |
| 81 | + "\n", |
| 82 | + "# Continue party planning until the user decides to stop\n", |
| 83 | + "while lets_party == 'y':\n", |
| 84 | + " # Get input from the user: number of cookies and number of people\n", |
| 85 | + " cookies = int(input(\"How many cookies are you baking? \"))\n", |
| 86 | + " people = int(input(\"How many people are attending? \"))\n", |
| 87 | + "\n", |
| 88 | + " # Calculate cookie distribution using the party_planner function\n", |
| 89 | + " cookies_each, leftovers = party_planner(cookies, people)\n", |
| 90 | + "\n", |
| 91 | + " if cookies_each: # If cookies_each is not None (i.e., people > 0)\n", |
| 92 | + " # Print the party details\n", |
| 93 | + " message = \"\\nLet's party! We'll have {} people attending, they'll each get to eat {} cookies, and we'll have {} left over.\"\n", |
| 94 | + " print(message.format(people, cookies_each, leftovers))\n", |
| 95 | + "\n", |
| 96 | + " # Ask if the user wants to continue party planning\n", |
| 97 | + " lets_party = input(\"\\nWould you like to party more? (y or n) \")" |
| 98 | + ] |
| 99 | + }, |
| 100 | + { |
| 101 | + "cell_type": "markdown", |
| 102 | + "id": "aaaa4f46-3f5f-4245-b5c6-aa400c548d1f", |
| 103 | + "metadata": {}, |
| 104 | + "source": [ |
| 105 | + "# No Specific Errors\n", |
| 106 | + "## except Exception as e:" |
| 107 | + ] |
| 108 | + }, |
| 109 | + { |
| 110 | + "cell_type": "code", |
| 111 | + "execution_count": 3, |
| 112 | + "id": "2c45c8e0-c0b4-4091-a790-fe809f3dbb2b", |
| 113 | + "metadata": {}, |
| 114 | + "outputs": [ |
| 115 | + { |
| 116 | + "name": "stdout", |
| 117 | + "output_type": "stream", |
| 118 | + "text": [ |
| 119 | + "How many cookies are you baking? 8\n", |
| 120 | + "How many people are attending? 3\n" |
| 121 | + ] |
| 122 | + }, |
| 123 | + { |
| 124 | + "name": "stdout", |
| 125 | + "output_type": "stream", |
| 126 | + "text": [ |
| 127 | + "\n", |
| 128 | + "Let's party! We'll have 3 people attending, they'll each get to eat 2 cookies, and we'll have 2 left over.\n" |
| 129 | + ] |
| 130 | + }, |
| 131 | + { |
| 132 | + "name": "stdout", |
| 133 | + "output_type": "stream", |
| 134 | + "text": [ |
| 135 | + "\n", |
| 136 | + "Would you like to party more? (y or n) y\n", |
| 137 | + "How many cookies are you baking? 8\n", |
| 138 | + "How many people are attending? 0\n" |
| 139 | + ] |
| 140 | + }, |
| 141 | + { |
| 142 | + "name": "stdout", |
| 143 | + "output_type": "stream", |
| 144 | + "text": [ |
| 145 | + "Exception occurred: integer division or modulo by zero\n" |
| 146 | + ] |
| 147 | + }, |
| 148 | + { |
| 149 | + "name": "stdout", |
| 150 | + "output_type": "stream", |
| 151 | + "text": [ |
| 152 | + "\n", |
| 153 | + "Would you like to party more? (y or n) y\n", |
| 154 | + "How many cookies are you baking? 0\n", |
| 155 | + "How many people are attending? 3\n", |
| 156 | + "\n", |
| 157 | + "Would you like to party more? (y or n) n\n" |
| 158 | + ] |
| 159 | + } |
| 160 | + ], |
| 161 | + "source": [ |
| 162 | + "# Define a function called \"party_planner\" to calculate cookie distribution\n", |
| 163 | + "def party_planner(cookies, people):\n", |
| 164 | + " # Initialize variables for leftovers and cookies per person\n", |
| 165 | + " leftovers = None\n", |
| 166 | + " num_each = None\n", |
| 167 | + "\n", |
| 168 | + " try:\n", |
| 169 | + " # Calculate the number of cookies each person will get\n", |
| 170 | + " num_each = cookies // people\n", |
| 171 | + " # Calculate the remaining cookies (leftovers)\n", |
| 172 | + " leftovers = cookies % people\n", |
| 173 | + " except Exception as e:\n", |
| 174 | + " print(\"Exception occurred: {}\".format(e))\n", |
| 175 | + "\n", |
| 176 | + " # Return the results\n", |
| 177 | + " return num_each, leftovers\n", |
| 178 | + "\n", |
| 179 | + "# Initialize a flag for party continuation\n", |
| 180 | + "lets_party = 'y'\n", |
| 181 | + "\n", |
| 182 | + "# Continue party planning until the user decides to stop\n", |
| 183 | + "while lets_party == 'y':\n", |
| 184 | + " # Get input from the user: number of cookies and number of people\n", |
| 185 | + " cookies = int(input(\"How many cookies are you baking? \"))\n", |
| 186 | + " people = int(input(\"How many people are attending? \"))\n", |
| 187 | + "\n", |
| 188 | + " # Calculate cookie distribution using the party_planner function\n", |
| 189 | + " cookies_each, leftovers = party_planner(cookies, people)\n", |
| 190 | + "\n", |
| 191 | + " if cookies_each: # If cookies_each is not None (i.e., people > 0)\n", |
| 192 | + " # Print the party details\n", |
| 193 | + " message = \"\\nLet's party! We'll have {} people attending, they'll each get to eat {} cookies, and we'll have {} left over.\"\n", |
| 194 | + " print(message.format(people, cookies_each, leftovers))\n", |
| 195 | + "\n", |
| 196 | + " # Ask if the user wants to continue party planning\n", |
| 197 | + " lets_party = input(\"\\nWould you like to party more? (y or n) \")" |
| 198 | + ] |
| 199 | + }, |
| 200 | + { |
| 201 | + "cell_type": "code", |
| 202 | + "execution_count": 1, |
| 203 | + "id": "10929d6f-85c4-424c-8c21-550cecedd99c", |
| 204 | + "metadata": {}, |
| 205 | + "outputs": [ |
| 206 | + { |
| 207 | + "name": "stdout", |
| 208 | + "output_type": "stream", |
| 209 | + "text": [ |
| 210 | + "Selected Jupyter core packages...\n", |
| 211 | + "IPython : 8.25.0\n", |
| 212 | + "ipykernel : 6.28.0\n", |
| 213 | + "ipywidgets : 8.1.2\n", |
| 214 | + "jupyter_client : 8.6.0\n", |
| 215 | + "jupyter_core : 5.5.0\n", |
| 216 | + "jupyter_server : 2.10.0\n", |
| 217 | + "jupyterlab : 4.2.3\n", |
| 218 | + "nbclient : 0.8.0\n", |
| 219 | + "nbconvert : 7.10.0\n", |
| 220 | + "nbformat : 5.9.2\n", |
| 221 | + "notebook : 7.2.1\n", |
| 222 | + "qtconsole : 5.5.1\n", |
| 223 | + "traitlets : 5.14.3\n" |
| 224 | + ] |
| 225 | + } |
| 226 | + ], |
| 227 | + "source": [ |
| 228 | + "!jupyter --version" |
| 229 | + ] |
| 230 | + }, |
| 231 | + { |
| 232 | + "cell_type": "code", |
| 233 | + "execution_count": 1, |
| 234 | + "id": "d556a636", |
| 235 | + "metadata": {}, |
| 236 | + "outputs": [ |
| 237 | + { |
| 238 | + "name": "stdout", |
| 239 | + "output_type": "stream", |
| 240 | + "text": [ |
| 241 | + "Python 3.12.2\n" |
| 242 | + ] |
| 243 | + } |
| 244 | + ], |
| 245 | + "source": [ |
| 246 | + "!python --version" |
| 247 | + ] |
| 248 | + } |
| 249 | + ], |
| 250 | + "metadata": { |
| 251 | + "kernelspec": { |
| 252 | + "display_name": "example", |
| 253 | + "language": "python", |
| 254 | + "name": "python3" |
| 255 | + }, |
| 256 | + "language_info": { |
| 257 | + "codemirror_mode": { |
| 258 | + "name": "ipython", |
| 259 | + "version": 3 |
| 260 | + }, |
| 261 | + "file_extension": ".py", |
| 262 | + "mimetype": "text/x-python", |
| 263 | + "name": "python", |
| 264 | + "nbconvert_exporter": "python", |
| 265 | + "pygments_lexer": "ipython3", |
| 266 | + "version": "3.12.2" |
| 267 | + } |
| 268 | + }, |
| 269 | + "nbformat": 4, |
| 270 | + "nbformat_minor": 5 |
| 271 | +} |
0 commit comments