From 6c5a2f9e41b774149704aa8192f6e6cc67df06ff Mon Sep 17 00:00:00 2001 From: manufranso Date: Thu, 15 Jan 2026 18:14:52 +0100 Subject: [PATCH 1/2] Update lab-python-error-handling.ipynb --- lab-python-error-handling.ipynb | 240 +++++++++++++++++++++++++++++++- 1 file changed, 236 insertions(+), 4 deletions(-) diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index 3e50ef8..b436bd5 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -41,18 +41,250 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 29, + "id": "0050200b", + "metadata": {}, + "outputs": [], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n" + ] + }, + { + "cell_type": "code", + "execution_count": 32, "id": "cc2c441d-9dcf-4817-b097-cf6cbe440846", "metadata": {}, "outputs": [], "source": [ - "# your code goes here" + "def initialize_inventory(products):\n", + " inventory = {}\n", + "\n", + " for product in products:\n", + " try:\n", + " quantity = int(input(f\"Enter a quantity of {product}: \"))\n", + " \n", + " if quantity < 0:\n", + " raise ValueError(\"Invalid quantity! Please enter a non-negative value.\")\n", + " \n", + " inventory[product] = quantity\n", + " \n", + " except ValueError as error:\n", + " print(f\"Error: {error}\")\n", + " inventory[product] = 0\n", + "\n", + " return inventory\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "eb9c615c", + "metadata": {}, + "outputs": [], + "source": [ + "def get_customer_order():\n", + " customer_orders = set()\n", + "\n", + " while True:\n", + " try:\n", + " order = input(\"Enter a product to order or 'exit' to finish: \")\n", + "\n", + " if order == \"exit\":\n", + " break\n", + " if not order:\n", + " raise ValueError(\"Order name cannot be empty.\")\n", + " if order not in inventory:\n", + " raise ValueError(f\"Product '{order}' doesnt exits\")\n", + "\n", + "\n", + " customer_orders.add(order)\n", + "\n", + " except ValueError as error:\n", + " print(f\"Error: {error}\")\n", + "\n", + " except KeyboardInterrupt:\n", + " print(\"\\nOperation cancelled\") \n", + " break \n", + "\n", + " return customer_orders" + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "id": "63f944e1", + "metadata": {}, + "outputs": [], + "source": [ + "def calculate_total_price(customer_orders, products):\n", + " total_products_ordered = len(customer_orders)\n", + " try:\n", + " percentage = (total_products_ordered / len(products)) *100\n", + " except ZeroDivisionError:\n", + " print(\"Error: No products to calculate percentage\")\n", + " percentage = 0\n", + " else:\n", + " print(\"Statistics calculated\")\n", + " \n", + " return total_products_ordered, percentage " + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "id": "e127e618", + "metadata": {}, + "outputs": [], + "source": [ + "def update_inventory(customer_orders, inventory):\n", + " for order in customer_orders:\n", + " try:\n", + " if order not in inventory:\n", + " raise KeyError(f\"Product {order} not found\")\n", + " if inventory[order] > 0:\n", + " inventory[order] -= 1\n", + " else:\n", + " print(f\"Product out of stock\")\n", + " except KeyError as error:\n", + " print(f\"Error: {error}\")\n", + " return inventory\n" + ] + }, + { + "cell_type": "code", + "execution_count": 51, + "id": "da6be2ec", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 1, 'mug': 2, 'hat': 3, 'book': 4, 'keychain': 5}\n" + ] + } + ], + "source": [ + "inventory = initialize_inventory (products)\n", + "print(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 52, + "id": "a3181dee", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: Product 'cat' doesnt exits\n", + "{'hat', 'mug', 'book'}\n" + ] + } + ], + "source": [ + "customer_orders = get_customer_order()\n", + "print(customer_orders)" + ] + }, + { + "cell_type": "code", + "execution_count": 53, + "id": "82553983", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 1, 'mug': 1, 'hat': 2, 'book': 3, 'keychain': 5}\n" + ] + } + ], + "source": [ + "updated_inventory = update_inventory(customer_orders, inventory)\n", + "print(updated_inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 54, + "id": "4809157c", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Statistics calculated\n", + "(3, 60.0)\n" + ] + } + ], + "source": [ + "order_statistics = calculate_total_price(customer_orders, products)\n", + "print(order_statistics)" + ] + }, + { + "cell_type": "code", + "execution_count": 55, + "id": "0578169a", + "metadata": {}, + "outputs": [], + "source": [ + "def print_order_statistics (order_statistics):\n", + " total_products_ordered, percentage = order_statistics\n", + "\n", + " print(f\"Total products ordered: {total_products_ordered}\")\n", + " print(f\"Percentage of products ordered: {percentage:.2f}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 56, + "id": "3381fbd5", + "metadata": {}, + "outputs": [], + "source": [ + "def print_updated_inventory(inventory):\n", + " print(\"Updated Inventory:\")\n", + " for product, quantity in inventory.items():\n", + " print(f\"{product}: {quantity}\") " + ] + }, + { + "cell_type": "code", + "execution_count": 57, + "id": "c3122461", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Total products ordered: 3\n", + "Percentage of products ordered: 60.00\n", + "Updated Inventory:\n", + "t-shirt: 1\n", + "mug: 1\n", + "hat: 2\n", + "book: 3\n", + "keychain: 5\n" + ] + } + ], + "source": [ + "final_statistics = print_order_statistics(order_statistics)\n", + "final_updated_inventory = print_updated_inventory(inventory)" ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, @@ -66,7 +298,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.5" } }, "nbformat": 4, From 9bec1eddb1048c634a076f8d83a68d8ed4fe9670 Mon Sep 17 00:00:00 2001 From: manufranso Date: Thu, 15 Jan 2026 18:42:51 +0100 Subject: [PATCH 2/2] Update lab-python-error-handling.ipynb --- lab-python-error-handling.ipynb | 100 +++++++++++++++++++++++++++++--- 1 file changed, 92 insertions(+), 8 deletions(-) diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index b436bd5..fa9bb13 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -41,7 +41,17 @@ }, { "cell_type": "code", - "execution_count": 29, + "execution_count": 39, + "id": "8cd86061", + "metadata": {}, + "outputs": [], + "source": [ + "import string" + ] + }, + { + "cell_type": "code", + "execution_count": 40, "id": "0050200b", "metadata": {}, "outputs": [], @@ -51,7 +61,7 @@ }, { "cell_type": "code", - "execution_count": 32, + "execution_count": 38, "id": "cc2c441d-9dcf-4817-b097-cf6cbe440846", "metadata": {}, "outputs": [], @@ -77,7 +87,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 41, "id": "eb9c615c", "metadata": {}, "outputs": [], @@ -93,7 +103,7 @@ " break\n", " if not order:\n", " raise ValueError(\"Order name cannot be empty.\")\n", - " if order not in inventory:\n", + " if order not in customer_orders:\n", " raise ValueError(f\"Product '{order}' doesnt exits\")\n", "\n", "\n", @@ -111,7 +121,7 @@ }, { "cell_type": "code", - "execution_count": 35, + "execution_count": 42, "id": "63f944e1", "metadata": {}, "outputs": [], @@ -131,7 +141,7 @@ }, { "cell_type": "code", - "execution_count": 37, + "execution_count": 43, "id": "e127e618", "metadata": {}, "outputs": [], @@ -152,7 +162,80 @@ }, { "cell_type": "code", - "execution_count": 51, + "execution_count": 44, + "id": "b37f7a27", + "metadata": {}, + "outputs": [], + "source": [ + "def remove_punctuation(sentence):\n", + " if not isinstance(sentence,str):\n", + " raise ValueError(\"Input must be a string.\")\n", + " return sentence.translate(str.maketrans(\"\", \"\", string.punctuation))" + ] + }, + { + "cell_type": "code", + "execution_count": 45, + "id": "af6d4fbc", + "metadata": {}, + "outputs": [], + "source": [ + "def word_count(sentence):\n", + " try:\n", + " sentence = remove_punctuation(sentence)\n", + " words = sentence.split()\n", + " except ValueError as error:\n", + " print(error)\n", + " else: \n", + " return len(words)\n", + " finally:\n", + " print(\"Execution complete\")" + ] + }, + { + "cell_type": "code", + "execution_count": 46, + "id": "17d8e01a", + "metadata": {}, + "outputs": [], + "source": [ + "def divide (x, y):\n", + " try:\n", + " result = x / y\n", + " except TypeError:\n", + " raise TypeError(\"Invalid arguments\")\n", + " except ZeroDivisionError:\n", + " raise ZeroDivisionError(\"Cannot divide by zero\")\n", + " else:\n", + " return result\n", + " finally:\n", + " print(\"Division executed\")\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 47, + "id": "b3de9425", + "metadata": {}, + "outputs": [], + "source": [ + "def fibonacci(number):\n", + " try:\n", + " if number < 0:\n", + " raise ValueError(\"The input must be non-negative\")\n", + " if number <=1:\n", + " return number\n", + " else :\n", + " return fibonacci(number - 1) + fibonacci(number - 2)\n", + " except TypeError:\n", + " raise TypeError(\"Must be an integer\")\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 48, "id": "da6be2ec", "metadata": {}, "outputs": [ @@ -160,7 +243,8 @@ "name": "stdout", "output_type": "stream", "text": [ - "{'t-shirt': 1, 'mug': 2, 'hat': 3, 'book': 4, 'keychain': 5}\n" + "Error: invalid literal for int() with base 10: 'd'\n", + "{'t-shirt': 0, 'mug': 5, 'hat': 5, 'book': 5, 'keychain': 5}\n" ] } ],