From f650f7dd91e77fe2044151b031311cc5436efed6 Mon Sep 17 00:00:00 2001 From: Victoria Cano Date: Thu, 19 Feb 2026 18:09:27 +0100 Subject: [PATCH 1/2] Updated Solutions for Lab --- lab-python-error-handling.ipynb | 113 +++++++++++++++++++++++++++++++- 1 file changed, 110 insertions(+), 3 deletions(-) diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index f4c6ef6..0e24dcb 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -72,13 +72,120 @@ "\n", "4. Test your code by running the program and deliberately entering invalid quantities and product names. Make sure the error handling mechanism works as expected.\n" ] + }, + { + "cell_type": "code", + "execution_count": 26, + "id": "d7b3e227-93a6-48b9-85c6-a0a366c2e8b3", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the price of hat: -10\n", + "Enter the price of keychain: 9\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Invalid input. Prices cannot be negative.\n" + ] + }, + { + "data": { + "text/plain": [ + "0" + ] + }, + "execution_count": 26, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def calculate_total_price(customer_orders):\n", + " try:\n", + " prices = {product: float(input(f\"Enter the price of {product}: \")) for product in customer_orders}\n", + "\n", + " if any(price < 0 or price == str for price in prices.values()):\n", + " print(\"Invalid input. Prices cannot be negative.\")\n", + " return 0\n", + "\n", + " except ValueError:\n", + " print(\"Invalid input. Please enter numeric values for prices.\")\n", + " return 0\n", + " else:\n", + " return sum(prices.values())\n", + " \n", + "orders = [\"hat\", \"keychain\"]\n", + "calculate_total_price(orders)\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "id": "535fca64-9d90-419e-9263-31983f525de9", + "metadata": {}, + "outputs": [], + "source": [ + "def get_customer_orders(inventory):\n", + " \n", + " try:\n", + " orders = int(input(\"Enter the number of customer orders: \"))\n", + " if orders < 0:\n", + " raise ValueError\n", + " \n", + " except ValueError:\n", + " print(\"Invalid input. Please enter a non-negative number.\")\n", + " return get_customer_orders(inventory)\n", + "\n", + " orders = []\n", + "\n", + "\n", + "def get_valid_product():\n", + " try:\n", + " product = input(\"Enter the name of a product that a customer wants to order: \").lower()\n", + "\n", + " if product not in inventory:\n", + " raise KeyError\n", + "\n", + " if inventory[product] <= 0:\n", + " raise ValueError\n", + "\n", + " return product\n", + "\n", + " except KeyError:\n", + " print(\"Invalid product name. Please choose a product from inventory.\")\n", + " return get_valid_product()\n", + "\n", + " except ValueError:\n", + " print(\"This product is out of stock. Please choose another product.\")\n", + " return get_valid_product()\n", + "\n", + " for _ in range(num_orders):\n", + " orders.append(get_valid_product())\n", + "\n", + " return orders\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f8fc2ec0-04c6-469f-8455-aff10012355d", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python [conda env:base] *", "language": "python", - "name": "python3" + "name": "conda-base-py" }, "language_info": { "codemirror_mode": { @@ -90,7 +197,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.9" } }, "nbformat": 4, From e8a89f78636d28922f2b17d2a00f929751191a5f Mon Sep 17 00:00:00 2001 From: Victoria Cano Date: Thu, 19 Feb 2026 18:13:06 +0100 Subject: [PATCH 2/2] Updated Solutions Lab Error Handling --- lab-python-error-handling.ipynb | 78 +++++++++++++++++++-------------- 1 file changed, 45 insertions(+), 33 deletions(-) diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index 0e24dcb..ed94d88 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -127,55 +127,67 @@ }, { "cell_type": "code", - "execution_count": 27, - "id": "535fca64-9d90-419e-9263-31983f525de9", + "execution_count": 73, + "id": "f8fc2ec0-04c6-469f-8455-aff10012355d", "metadata": {}, - "outputs": [], + "outputs": [ + { + "ename": "KeyboardInterrupt", + "evalue": "Interrupted by user", + "output_type": "error", + "traceback": [ + "\u001b[31m---------------------------------------------------------------------------\u001b[39m", + "\u001b[31mKeyboardInterrupt\u001b[39m Traceback (most recent call last)", + "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[73]\u001b[39m\u001b[32m, line 34\u001b[39m\n\u001b[32m 30\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m customer_orders\n\u001b[32m 32\u001b[39m inventory = {\u001b[33m'\u001b[39m\u001b[33mt-shirt\u001b[39m\u001b[33m'\u001b[39m: \u001b[32m3\u001b[39m, \u001b[33m'\u001b[39m\u001b[33mmug\u001b[39m\u001b[33m'\u001b[39m: \u001b[32m0\u001b[39m, \u001b[33m'\u001b[39m\u001b[33mhat\u001b[39m\u001b[33m'\u001b[39m: \u001b[32m1\u001b[39m, \u001b[33m'\u001b[39m\u001b[33mbook\u001b[39m\u001b[33m'\u001b[39m: \u001b[32m3\u001b[39m, \u001b[33m'\u001b[39m\u001b[33mkeychain\u001b[39m\u001b[33m'\u001b[39m: \u001b[32m4\u001b[39m}\n\u001b[32m---> \u001b[39m\u001b[32m34\u001b[39m orders = \u001b[43mget_customer_orders\u001b[49m\u001b[43m(\u001b[49m\u001b[43minventory\u001b[49m\u001b[43m)\u001b[49m\n\u001b[32m 35\u001b[39m \u001b[38;5;28mprint\u001b[39m(orders)\n", + "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[73]\u001b[39m\u001b[32m, line 5\u001b[39m, in \u001b[36mget_customer_orders\u001b[39m\u001b[34m(inventory)\u001b[39m\n\u001b[32m 3\u001b[39m \u001b[38;5;28;01mwhile\u001b[39;00m \u001b[38;5;28;01mTrue\u001b[39;00m:\n\u001b[32m 4\u001b[39m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[32m----> \u001b[39m\u001b[32m5\u001b[39m num_orders = \u001b[38;5;28mint\u001b[39m(\u001b[38;5;28;43minput\u001b[39;49m\u001b[43m(\u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43mEnter the number of customer orders: \u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m)\u001b[49m)\n\u001b[32m 6\u001b[39m \u001b[38;5;28;01mif\u001b[39;00m num_orders < \u001b[32m0\u001b[39m:\n\u001b[32m 7\u001b[39m \u001b[38;5;28mprint\u001b[39m(\u001b[33m\"\u001b[39m\u001b[33mInvalid input: number must be positive.\u001b[39m\u001b[33m\"\u001b[39m)\n", + "\u001b[36mFile \u001b[39m\u001b[32m/opt/anaconda3/lib/python3.13/site-packages/ipykernel/kernelbase.py:1275\u001b[39m, in \u001b[36mKernel.raw_input\u001b[39m\u001b[34m(self, prompt)\u001b[39m\n\u001b[32m 1273\u001b[39m msg = \u001b[33m\"\u001b[39m\u001b[33mraw_input was called, but this frontend does not support input requests.\u001b[39m\u001b[33m\"\u001b[39m\n\u001b[32m 1274\u001b[39m \u001b[38;5;28;01mraise\u001b[39;00m StdinNotImplementedError(msg)\n\u001b[32m-> \u001b[39m\u001b[32m1275\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[43m.\u001b[49m\u001b[43m_input_request\u001b[49m\u001b[43m(\u001b[49m\n\u001b[32m 1276\u001b[39m \u001b[43m \u001b[49m\u001b[38;5;28;43mstr\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mprompt\u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 1277\u001b[39m \u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m.\u001b[49m\u001b[43m_parent_ident\u001b[49m\u001b[43m[\u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43mshell\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 1278\u001b[39m \u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m.\u001b[49m\u001b[43mget_parent\u001b[49m\u001b[43m(\u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43mshell\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 1279\u001b[39m \u001b[43m \u001b[49m\u001b[43mpassword\u001b[49m\u001b[43m=\u001b[49m\u001b[38;5;28;43;01mFalse\u001b[39;49;00m\u001b[43m,\u001b[49m\n\u001b[32m 1280\u001b[39m \u001b[43m\u001b[49m\u001b[43m)\u001b[49m\n", + "\u001b[36mFile \u001b[39m\u001b[32m/opt/anaconda3/lib/python3.13/site-packages/ipykernel/kernelbase.py:1320\u001b[39m, in \u001b[36mKernel._input_request\u001b[39m\u001b[34m(self, prompt, ident, parent, password)\u001b[39m\n\u001b[32m 1317\u001b[39m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mKeyboardInterrupt\u001b[39;00m:\n\u001b[32m 1318\u001b[39m \u001b[38;5;66;03m# re-raise KeyboardInterrupt, to truncate traceback\u001b[39;00m\n\u001b[32m 1319\u001b[39m msg = \u001b[33m\"\u001b[39m\u001b[33mInterrupted by user\u001b[39m\u001b[33m\"\u001b[39m\n\u001b[32m-> \u001b[39m\u001b[32m1320\u001b[39m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mKeyboardInterrupt\u001b[39;00m(msg) \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mNone\u001b[39;00m\n\u001b[32m 1321\u001b[39m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mException\u001b[39;00m:\n\u001b[32m 1322\u001b[39m \u001b[38;5;28mself\u001b[39m.log.warning(\u001b[33m\"\u001b[39m\u001b[33mInvalid Message:\u001b[39m\u001b[33m\"\u001b[39m, exc_info=\u001b[38;5;28;01mTrue\u001b[39;00m)\n", + "\u001b[31mKeyboardInterrupt\u001b[39m: Interrupted by user" + ] + } + ], "source": [ "def get_customer_orders(inventory):\n", - " \n", - " try:\n", - " orders = int(input(\"Enter the number of customer orders: \"))\n", - " if orders < 0:\n", - " raise ValueError\n", - " \n", - " except ValueError:\n", - " print(\"Invalid input. Please enter a non-negative number.\")\n", - " return get_customer_orders(inventory)\n", "\n", - " orders = []\n", + " while True:\n", + " try:\n", + " num_orders = int(input(\"Enter the number of customer orders: \"))\n", + " if num_orders < 0:\n", + " print(\"Invalid input: number must be positive.\")\n", + " elif num_orders == 0:\n", + " print(\"No customer orders\")\n", + " else:\n", + " break\n", + " except ValueError:\n", + " print(\"Invalid input: please enter a numeric value.\")\n", "\n", + " customer_orders = set()\n", "\n", - "def get_valid_product():\n", - " try:\n", - " product = input(\"Enter the name of a product that a customer wants to order: \").lower()\n", "\n", - " if product not in inventory:\n", - " raise KeyError\n", + " for i in range(num_orders):\n", + " while True:\n", + " product = input(f\"Enter the name of product #{i + 1}: \")\n", "\n", - " if inventory[product] <= 0:\n", - " raise ValueError\n", + " if product not in inventory:\n", + " print(\"Invalid product: not in inventory.\")\n", + " elif inventory[product] <= 0:\n", + " print(\"Invalid product: out of stock.\")\n", + " else:\n", + " customer_orders.add(product)\n", + " break\n", "\n", - " return product\n", + " return customer_orders\n", "\n", - " except KeyError:\n", - " print(\"Invalid product name. Please choose a product from inventory.\")\n", - " return get_valid_product()\n", + "inventory = {'t-shirt': 3, 'mug': 0, 'hat': 1, 'book': 3, 'keychain': 4}\n", "\n", - " except ValueError:\n", - " print(\"This product is out of stock. Please choose another product.\")\n", - " return get_valid_product()\n", - "\n", - " for _ in range(num_orders):\n", - " orders.append(get_valid_product())\n", - "\n", - " return orders\n" + "orders = get_customer_orders(inventory)\n", + "print(orders)\n" ] }, { "cell_type": "code", "execution_count": null, - "id": "f8fc2ec0-04c6-469f-8455-aff10012355d", + "id": "4e56e926-cfc6-4ce5-9693-f4291fb3a4b4", "metadata": {}, "outputs": [], "source": []