From 81d4e8c81bff81c44ee68762120701be3e151a3f Mon Sep 17 00:00:00 2001 From: gabrieleb76-bot Date: Fri, 17 Apr 2026 08:57:19 +0100 Subject: [PATCH 1/2] Error_handling_Gabriel_dia4 --- lab-python-error-handling.ipynb | 245 +++++++++++++++++++++++++++++++- 1 file changed, 243 insertions(+), 2 deletions(-) diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index f4c6ef6..afbe55d 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -72,11 +72,252 @@ "\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": 3, + "id": "9a5ff580", + "metadata": {}, + "outputs": [], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "5bc92a63", + "metadata": {}, + "outputs": [], + "source": [ + "def initialize_inventory(products):\n", + " inventory ={}\n", + " \n", + " for product in products:\n", + " valid_input = False\n", + " \n", + " while not valid_input:\n", + " try:\n", + " quantity = int(input(f\"Enter the quantity of {product}s available: \"))\n", + " if quantity < 0:\n", + " print (\"quantity cannot be negative. Please enter a valid number.\")\n", + " else:\n", + " inventory [product] = quantity\n", + " valid_input = True\n", + " \n", + " except ValueError:\n", + " print(\"Invalid input. Please enter a valid number.\")\n", + " \n", + " return inventory " + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "6e1e6966", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 5, 'mug': 4, 'hat': 3, 'book': 2, 'keychain': 1}\n" + ] + } + ], + "source": [ + "inventory = initialize_inventory(products)\n", + "print(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "id": "7d18849b", + "metadata": {}, + "outputs": [], + "source": [ + "def get_customer_orders(inventory):\n", + " valid_orders_input = False\n", + " \n", + " while not valid_orders_input:\n", + " try:\n", + " number_of_orders = int(input(\"Enter the number of customer orders: \"))\n", + " if number_of_orders < 0:\n", + " print(\"Number of orders cannot be negative. Please enter a valid number.\")\n", + " else:\n", + " valid_orders_input = True\n", + " except ValueError:\n", + " print(\"Invalid input. Please enter a valid number.\")\n", + "\n", + " customer_orders = set()\n", + " \n", + " while len(customer_orders) < number_of_orders:\n", + " product = input(\"Enter the name of a product that a customer wants to order: \")\n", + " if product not in inventory:\n", + " print(\"This product does not exist in the inventory. Please enter a valid product.\")\n", + " elif inventory[product] <= 0:\n", + " print(\"This product is out of stock. Please choose another product.\")\n", + " else:\n", + " customer_orders.add(product)\n", + " \n", + " return customer_orders" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "8cdaf00f", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'mug', 'book', 'hat'}\n" + ] + } + ], + "source": [ + "customer_orders = get_customer_order(inventory)\n", + "print(customer_orders)" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "a7578e74", + "metadata": {}, + "outputs": [], + "source": [ + "def calculate_order_statistics(customer_orders, products):\n", + " total_products_ordered = len(customer_orders)\n", + " percentage_ordered = (total_products_ordered / len(products)) * 100\n", + " return total_products_ordered, percentage_ordered" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "id": "368a410b", + "metadata": {}, + "outputs": [], + "source": [ + "def print_order_statistics(order_statistics):\n", + " print(\"\\nOrder Statistics:\")\n", + " print(f\"Total Products Ordered: {order_statistics[0]}\")\n", + " print(f\"Percentage of Unique Products Ordered: {order_statistics[1]}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "id": "50a94cbe", + "metadata": {}, + "outputs": [], + "source": [ + "def update_inventory(customer_orders, inventory):\n", + " updated_inventory = {\n", + " product: quantity - 1 if product in customer_orders else quantity\n", + " for product, quantity in inventory.items()\n", + " if (quantity - 1 if product in customer_orders else quantity) > 0\n", + " }\n", + " return updated_inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "id": "2b5f0b94", + "metadata": {}, + "outputs": [], + "source": [ + "def calculate_total_price(customer_orders):\n", + " total_price = 0.0\n", + " \n", + " for product in customer_orders:\n", + " valid_price = False\n", + " \n", + " while not valid_price:\n", + " try:\n", + " price = float(input(f\"Enter the price of {product}: \"))\n", + " \n", + " if price < 0:\n", + " print(\"Price cannot be negative. Please enter a valid price.\")\n", + " else:\n", + " total_price += price\n", + " valid_price = True\n", + " \n", + " except ValueError:\n", + " print(\"Invalid input. Please enter a valid price.\")\n", + " \n", + " return total_price" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "id": "a4996b31", + "metadata": {}, + "outputs": [], + "source": [ + "def print_updated_inventory(inventory):\n", + " print(\"\\nUpdated Inventory:\")\n", + " for product, quantity in inventory.items():\n", + " print(f\"{product}: {quantity}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "id": "3d55aa63", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Order Statistics:\n", + "Total Products Ordered: 3\n", + "Percentage of Unique Products Ordered: 60.0\n", + "\n", + "Updated Inventory:\n", + "t-shirt: 5\n", + "mug: 3\n", + "hat: 2\n", + "book: 1\n", + "keychain: 1\n", + "Total Price: 18.0\n" + ] + } + ], + "source": [ + "inventory = initialize_inventory(products)\n", + "customer_orders = get_customer_orders(inventory)\n", + "\n", + "order_statistics = calculate_order_statistics(customer_orders, products)\n", + "print_order_statistics(order_statistics)\n", + "\n", + "inventory = update_inventory(customer_orders, inventory)\n", + "print_updated_inventory(inventory)\n", + "\n", + "total_price = calculate_total_price(customer_orders)\n", + "print(f\"Total Price: {total_price}\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "51364811", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -90,7 +331,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.9.6" } }, "nbformat": 4, From 3f4d98cae4ef097dbbc80132c410b95e8fa9b4d9 Mon Sep 17 00:00:00 2001 From: gabrieleb76-bot Date: Fri, 17 Apr 2026 17:23:38 +0100 Subject: [PATCH 2/2] lab-python-error-handling.ipynb_CORRECTION --- lab-python-error-handling.ipynb | 159 ++++++++++++++++++-------------- 1 file changed, 91 insertions(+), 68 deletions(-) diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index afbe55d..f484bb8 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -75,7 +75,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 4, "id": "9a5ff580", "metadata": {}, "outputs": [], @@ -85,35 +85,34 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 1, "id": "5bc92a63", "metadata": {}, "outputs": [], "source": [ "def initialize_inventory(products):\n", - " inventory ={}\n", - " \n", + " inventory = {}\n", + "\n", " for product in products:\n", - " valid_input = False\n", - " \n", - " while not valid_input:\n", + " while True:\n", " try:\n", " quantity = int(input(f\"Enter the quantity of {product}s available: \"))\n", + "\n", " if quantity < 0:\n", - " print (\"quantity cannot be negative. Please enter a valid number.\")\n", - " else:\n", - " inventory [product] = quantity\n", - " valid_input = True\n", - " \n", - " except ValueError:\n", - " print(\"Invalid input. Please enter a valid number.\")\n", - " \n", + " raise ValueError(\"Quantity cannot be negative. Please enter a valid quantity.\")\n", + "\n", + " inventory[product] = quantity\n", + " break\n", + "\n", + " except ValueError as error:\n", + " print(f\"Error: {error}\")\n", + "\n", " return inventory " ] }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 5, "id": "6e1e6966", "metadata": {}, "outputs": [ @@ -132,41 +131,47 @@ }, { "cell_type": "code", - "execution_count": 21, + "execution_count": 6, "id": "7d18849b", "metadata": {}, "outputs": [], "source": [ "def get_customer_orders(inventory):\n", - " valid_orders_input = False\n", - " \n", - " while not valid_orders_input:\n", + " while True:\n", " try:\n", " number_of_orders = int(input(\"Enter the number of customer orders: \"))\n", + "\n", " if number_of_orders < 0:\n", - " print(\"Number of orders cannot be negative. Please enter a valid number.\")\n", - " else:\n", - " valid_orders_input = True\n", - " except ValueError:\n", - " print(\"Invalid input. Please enter a valid number.\")\n", + " raise ValueError(\"The number of orders cannot be negative. Please enter a valid number.\")\n", + "\n", + " break\n", + "\n", + " except ValueError as error:\n", + " print(f\"Error: {error}\")\n", "\n", " customer_orders = set()\n", - " \n", + "\n", " while len(customer_orders) < number_of_orders:\n", - " product = input(\"Enter the name of a product that a customer wants to order: \")\n", - " if product not in inventory:\n", - " print(\"This product does not exist in the inventory. Please enter a valid product.\")\n", - " elif inventory[product] <= 0:\n", - " print(\"This product is out of stock. Please choose another product.\")\n", - " else:\n", + " try:\n", + " product = input(\"Enter the name of a product that a customer wants to order: \").strip().lower()\n", + "\n", + " if product not in inventory:\n", + " raise ValueError(\"This product does not exist in the inventory. Please enter a valid product.\")\n", + "\n", + " if inventory[product] <= 0:\n", + " raise ValueError(\"This product is out of stock. Please choose another product.\")\n", + "\n", " customer_orders.add(product)\n", - " \n", + "\n", + " except ValueError as error:\n", + " print(f\"Error: {error}\")\n", + "\n", " return customer_orders" ] }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 8, "id": "8cdaf00f", "metadata": {}, "outputs": [ @@ -174,12 +179,12 @@ "name": "stdout", "output_type": "stream", "text": [ - "{'mug', 'book', 'hat'}\n" + "{'t-shirt', 'book', 'keychain', 'hat'}\n" ] } ], "source": [ - "customer_orders = get_customer_order(inventory)\n", + "customer_orders = get_customer_orders(inventory)\n", "print(customer_orders)" ] }, @@ -198,79 +203,96 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 9, "id": "368a410b", "metadata": {}, "outputs": [], "source": [ "def print_order_statistics(order_statistics):\n", + " total_products_ordered, percentage_ordered = order_statistics\n", + "\n", " print(\"\\nOrder Statistics:\")\n", - " print(f\"Total Products Ordered: {order_statistics[0]}\")\n", - " print(f\"Percentage of Unique Products Ordered: {order_statistics[1]}\")" + " print(f\"Total Products Ordered: {total_products_ordered}\")\n", + " print(f\"Percentage of Unique Products Ordered: {percentage_ordered:.1f}%\")" ] }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 10, "id": "50a94cbe", "metadata": {}, "outputs": [], "source": [ "def update_inventory(customer_orders, inventory):\n", - " updated_inventory = {\n", - " product: quantity - 1 if product in customer_orders else quantity\n", - " for product, quantity in inventory.items()\n", - " if (quantity - 1 if product in customer_orders else quantity) > 0\n", - " }\n", + " updated_inventory = {}\n", + "\n", + " for product, quantity in inventory.items():\n", + " updated_quantity = quantity - 1 if product in customer_orders else quantity\n", + "\n", + " if updated_quantity > 0:\n", + " updated_inventory[product] = updated_quantity\n", + "\n", " return updated_inventory" ] }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 11, "id": "2b5f0b94", "metadata": {}, "outputs": [], "source": [ "def calculate_total_price(customer_orders):\n", " total_price = 0.0\n", - " \n", + "\n", " for product in customer_orders:\n", - " valid_price = False\n", - " \n", - " while not valid_price:\n", + " while True:\n", " try:\n", " price = float(input(f\"Enter the price of {product}: \"))\n", - " \n", + "\n", " if price < 0:\n", - " print(\"Price cannot be negative. Please enter a valid price.\")\n", - " else:\n", - " total_price += price\n", - " valid_price = True\n", - " \n", - " except ValueError:\n", - " print(\"Invalid input. Please enter a valid price.\")\n", - " \n", + " raise ValueError(\"Price cannot be negative. Please enter a valid price.\")\n", + "\n", + " total_price += price\n", + " break\n", + "\n", + " except ValueError as error:\n", + " print(f\"Error: {error}\")\n", + "\n", " return total_price" ] }, { "cell_type": "code", - "execution_count": 18, + "execution_count": 12, "id": "a4996b31", "metadata": {}, "outputs": [], "source": [ "def print_updated_inventory(inventory):\n", " print(\"\\nUpdated Inventory:\")\n", + "\n", " for product, quantity in inventory.items():\n", " print(f\"{product}: {quantity}\")" ] }, { "cell_type": "code", - "execution_count": 22, - "id": "3d55aa63", + "execution_count": 17, + "id": "1ccdf895", + "metadata": {}, + "outputs": [], + "source": [ + "def calculate_order_statistics(customer_orders, products):\n", + " total_products_ordered = len(customer_orders)\n", + " percentage_ordered = (total_products_ordered / len(products)) * 100\n", + " return total_products_ordered, percentage_ordered" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "id": "51364811", "metadata": {}, "outputs": [ { @@ -279,16 +301,17 @@ "text": [ "\n", "Order Statistics:\n", - "Total Products Ordered: 3\n", - "Percentage of Unique Products Ordered: 60.0\n", + "Total Products Ordered: 2\n", + "Percentage of Unique Products Ordered: 40.0%\n", "\n", "Updated Inventory:\n", "t-shirt: 5\n", "mug: 3\n", "hat: 2\n", - "book: 1\n", + "book: 2\n", "keychain: 1\n", - "Total Price: 18.0\n" + "\n", + "Total Price: $15.00\n" ] } ], @@ -303,13 +326,13 @@ "print_updated_inventory(inventory)\n", "\n", "total_price = calculate_total_price(customer_orders)\n", - "print(f\"Total Price: {total_price}\")" + "print(f\"\\nTotal Price: ${total_price:.2f}\")" ] }, { "cell_type": "code", "execution_count": null, - "id": "51364811", + "id": "495c97ca", "metadata": {}, "outputs": [], "source": []