From d2e2ddd08a456e4fe17da58f5f284acc9df1e112 Mon Sep 17 00:00:00 2001 From: Abanoub Barayo <62501003+abanoubdev@users.noreply.github.com> Date: Fri, 3 Apr 2026 11:00:08 +0200 Subject: [PATCH 1/6] Solved Lab --- lab-python-error-handling.ipynb | 91 +++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index f4c6ef6..75add03 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -72,6 +72,97 @@ "\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": null, + "id": "1436ad04", + "metadata": {}, + "outputs": [], + "source": [ + "import time\n", + "\n", + "def initialize_inventory(products):\n", + " try:\n", + " inventory = {}\n", + " for product in products:\n", + " count = int(input(f\"Enter the inventory count for {product}: \"))\n", + " if count < 0:\n", + " print(f\"Invalid input for {product}. Inventory count cannot be negative.\")\n", + " raise ValueError(\"Inventory count cannot be negative.\")\n", + " inventory[product] = count\n", + " return inventory\n", + " except ValueError as e:\n", + " print(f\"Input Error, please enter a valid number: {e}\")\n", + " return {}\n", + "\n", + "def get_customer_orders(products):\n", + " \n", + " try:\n", + " customer_orders = set()\n", + " while True:\n", + " order = input(f\"Please enter your Order (available products: {', '.join(products)}): \").strip().lower()\n", + " if order in products:\n", + " customer_orders.add(order)\n", + " else:\n", + " print(f\"Sorry, {order} is not in our product list.\")\n", + " continue\n", + " more_product = input(\"Do you want to order more products? (yes/no): \").strip().lower()\n", + " if more_product == \"no\":\n", + " break\n", + " return customer_orders\n", + " \n", + " except ValueError as e:\n", + " print(f\"Input Error, please enter a valid number: {e}\")\n", + " except Exception as e:\n", + " print(f\"An error occurred while getting customer orders: {e}\")\n", + " return set()\n", + "\n", + "def update_inventory(customer_orders, inventory):\n", + " print(\"Processing Orders...\")\n", + " for item in customer_orders:\n", + " if inventory.get(item, 0) > 0:\n", + " inventory[item] -= 1\n", + "\n", + "def calculate_order_statistics(customer_orders, products):\n", + " unique_products_ordered = len(customer_orders)\n", + " percentage_ordered = (unique_products_ordered / len(products)) * 100\n", + " return unique_products_ordered, percentage_ordered\n", + "\n", + "def print_order_statistics(order_status):\n", + " print(f\"Order Statistics:-\")\n", + " print(f\"Total Items Ordered: {order_status[0]}\")\n", + " print(f\"Percentage of Product Types Ordered: {order_status[1]:.2f}%\")\n", + "\n", + "def print_updated_inventory(inventory):\n", + " print(\"Updated Inventory:-\")\n", + " for product, quantity in inventory.items():\n", + " print(f\"{product.capitalize()}: {quantity}\")\n", + "\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", + "try:\n", + " inventory = initialize_inventory(products)\n", + " if not inventory:\n", + " print(\"Inventory initialization failed. Exiting program.\")\n", + " else:\n", + " customer_orders = get_customer_orders(products)\n", + " time.sleep(0.5)\n", + " if not customer_orders:\n", + " print(\"No valid orders entered.\")\n", + " else:\n", + " print(f\"Summary of items ordered: {customer_orders}\")\n", + " time.sleep(1)\n", + " total, percentage = calculate_order_statistics(customer_orders, products)\n", + " time.sleep(1)\n", + " print_order_statistics((total, percentage))\n", + " update_inventory(customer_orders, inventory)\n", + " time.sleep(1)\n", + " print_updated_inventory(inventory)\n", + "\n", + "except Exception as e:\n", + " print(f\"An unexpected error occurred: {e}\")" + ] } ], "metadata": { From 7d07821c359d7589d1bab72ab7dadbdef30e6f20 Mon Sep 17 00:00:00 2001 From: Abanoub Barayo <62501003+abanoubdev@users.noreply.github.com> Date: Fri, 3 Apr 2026 11:17:00 +0200 Subject: [PATCH 2/6] Solved Lab --- lab-python-error-handling.ipynb | 138 +++++++++++++++----------------- 1 file changed, 66 insertions(+), 72 deletions(-) diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index 75add03..406e505 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -80,94 +80,88 @@ "metadata": {}, "outputs": [], "source": [ - "import time\n", - "\n", "def initialize_inventory(products):\n", - " try:\n", - " inventory = {}\n", - " for product in products:\n", - " count = int(input(f\"Enter the inventory count for {product}: \"))\n", - " if count < 0:\n", - " print(f\"Invalid input for {product}. Inventory count cannot be negative.\")\n", - " raise ValueError(\"Inventory count cannot be negative.\")\n", - " inventory[product] = count\n", - " return inventory\n", - " except ValueError as e:\n", - " print(f\"Input Error, please enter a valid number: {e}\")\n", - " return {}\n", - "\n", - "def get_customer_orders(products):\n", - " \n", - " try:\n", - " customer_orders = set()\n", + " inventory = {}\n", + " for product in products:\n", " while True:\n", - " order = input(f\"Please enter your Order (available products: {', '.join(products)}): \").strip().lower()\n", - " if order in products:\n", - " customer_orders.add(order)\n", - " else:\n", - " print(f\"Sorry, {order} is not in our product list.\")\n", + " try:\n", + " count = int(input(f\"Enter the inventory count for {product}: \"))\n", + " if count < 0:\n", + " print(\"Error: Inventory count cannot be negative. Please try again.\")\n", + " continue\n", + " inventory[product] = count\n", + " break \n", + " except ValueError:\n", + " print(\"Error: Please enter a valid whole number for the count.\")\n", + " return inventory\n", + "\n", + "def get_customer_orders(inventory):\n", + "\n", + " customer_orders = []\n", + "\n", + " while True:\n", + " try:\n", + " num_orders = int(input(\"\\nHow many items would you like to order today? \"))\n", + " if num_orders < 0:\n", + " print(\"Error: You cannot order a negative number of items.\")\n", " continue\n", - " more_product = input(\"Do you want to order more products? (yes/no): \").strip().lower()\n", - " if more_product == \"no\":\n", + " break\n", + " except ValueError:\n", + " print(\"Error: Please enter a valid number for the quantity of orders.\")\n", + "\n", + " for i in range(num_orders):\n", + " while True:\n", + " order = input(f\"Enter item #{i+1} (Available: {', '.join(inventory.keys())}): \").strip().lower()\n", + " \n", + " if order not in inventory:\n", + " print(f\"Error: Sorry, '{order}' is not in our product list.\")\n", + " elif inventory[order] <= 0:\n", + " print(f\"Error: Sorry, '{order}' is currently out of stock!\")\n", + " else:\n", + " customer_orders.append(order)\n", " break\n", - " return customer_orders\n", - " \n", - " except ValueError as e:\n", - " print(f\"Input Error, please enter a valid number: {e}\")\n", - " except Exception as e:\n", - " print(f\"An error occurred while getting customer orders: {e}\")\n", - " return set()\n", + " \n", + " return customer_orders\n", "\n", "def update_inventory(customer_orders, inventory):\n", - " print(\"Processing Orders...\")\n", + " \"\"\"Updates the inventory based on items ordered.\"\"\"\n", + " print(\"\\nProcessing Orders...\")\n", " for item in customer_orders:\n", " if inventory.get(item, 0) > 0:\n", " inventory[item] -= 1\n", + " # If you want to remove the key entirely when it hits 0:\n", + " if inventory[item] == 0:\n", + " print(f\"Alert: {item} is now out of stock!\")\n", "\n", - "def calculate_order_statistics(customer_orders, products):\n", - " unique_products_ordered = len(customer_orders)\n", - " percentage_ordered = (unique_products_ordered / len(products)) * 100\n", + "def calculate_order_statistics(customer_orders, total_products_count):\n", + " unique_products_ordered = len(set(customer_orders))\n", + " percentage_ordered = (unique_products_ordered / total_products_count) * 100\n", " return unique_products_ordered, percentage_ordered\n", "\n", - "def print_order_statistics(order_status):\n", - " print(f\"Order Statistics:-\")\n", - " print(f\"Total Items Ordered: {order_status[0]}\")\n", - " print(f\"Percentage of Product Types Ordered: {order_status[1]:.2f}%\")\n", - "\n", - "def print_updated_inventory(inventory):\n", - " print(\"Updated Inventory:-\")\n", - " for product, quantity in inventory.items():\n", - " print(f\"{product.capitalize()}: {quantity}\")\n", - "\n", - "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", - "\n", - "try:\n", - " inventory = initialize_inventory(products)\n", - " if not inventory:\n", - " print(\"Inventory initialization failed. Exiting program.\")\n", - " else:\n", - " customer_orders = get_customer_orders(products)\n", - " time.sleep(0.5)\n", - " if not customer_orders:\n", - " print(\"No valid orders entered.\")\n", - " else:\n", - " print(f\"Summary of items ordered: {customer_orders}\")\n", - " time.sleep(1)\n", - " total, percentage = calculate_order_statistics(customer_orders, products)\n", - " time.sleep(1)\n", - " print_order_statistics((total, percentage))\n", - " update_inventory(customer_orders, inventory)\n", - " time.sleep(1)\n", - " print_updated_inventory(inventory)\n", - "\n", - "except Exception as e:\n", - " print(f\"An unexpected error occurred: {e}\")" + "def calculate_total_price(customer_orders):\n", + " total_price = 0\n", + " unique_orders = set(customer_orders)\n", + " \n", + " prices = {}\n", + " for item in unique_orders:\n", + " while True:\n", + " try:\n", + " price = float(input(f\"Enter the price for {item}: \"))\n", + " if price < 0:\n", + " print(\"Error: Price cannot be negative.\")\n", + " continue\n", + " prices[item] = price\n", + " break\n", + " except ValueError:\n", + " print(\"Error: Please enter a valid numeric value for the price.\")\n", + " for item in customer_orders:\n", + " total_price += prices[item]" ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, @@ -181,7 +175,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.12" } }, "nbformat": 4, From 7f20fb5c74f8997b7c55edcd29b43f3d280aac42 Mon Sep 17 00:00:00 2001 From: Abanoub Barayo <62501003+abanoubdev@users.noreply.github.com> Date: Fri, 3 Apr 2026 11:23:50 +0200 Subject: [PATCH 3/6] Solved Lab --- lab-python-error-handling.ipynb | 76 ++++++++++++++++++++++++++++++--- 1 file changed, 69 insertions(+), 7 deletions(-) diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index 406e505..65a9ba8 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -75,11 +75,39 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "id": "1436ad04", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: Inventory count cannot be negative. Please try again.\n", + "Error: Inventory count cannot be negative. Please try again.\n", + "\n", + "Initial Inventory: {'t-shirt': 88, 'mug': 22, 'hat': 33, 'book': 44, 'keychain': 90}\n", + "\n", + "Order Summary: ['mug', 't-shirt', 'hat']\n", + "Total Unique Products Ordered: 3\n", + "Percentage of Catalog Ordered: 60.00%\n", + "\n", + "Processing Orders...\n", + "\n", + "Final Transaction Total: $136.00\n", + "\n", + "Final Inventory Status:\n", + "- T-shirt: 87\n", + "- Mug: 21\n", + "- Hat: 32\n", + "- Book: 44\n", + "- Keychain: 90\n" + ] + } + ], "source": [ + "import time\n", + "\n", "def initialize_inventory(products):\n", " inventory = {}\n", " for product in products:\n", @@ -96,9 +124,7 @@ " return inventory\n", "\n", "def get_customer_orders(inventory):\n", - "\n", " customer_orders = []\n", - "\n", " while True:\n", " try:\n", " num_orders = int(input(\"\\nHow many items would you like to order today? \"))\n", @@ -124,21 +150,21 @@ " return customer_orders\n", "\n", "def update_inventory(customer_orders, inventory):\n", - " \"\"\"Updates the inventory based on items ordered.\"\"\"\n", " print(\"\\nProcessing Orders...\")\n", " for item in customer_orders:\n", " if inventory.get(item, 0) > 0:\n", " inventory[item] -= 1\n", - " # If you want to remove the key entirely when it hits 0:\n", " if inventory[item] == 0:\n", " print(f\"Alert: {item} is now out of stock!\")\n", "\n", "def calculate_order_statistics(customer_orders, total_products_count):\n", + " \"\"\"Calculates basic stats about the order.\"\"\"\n", " unique_products_ordered = len(set(customer_orders))\n", " percentage_ordered = (unique_products_ordered / total_products_count) * 100\n", " return unique_products_ordered, percentage_ordered\n", "\n", "def calculate_total_price(customer_orders):\n", + " \"\"\"Calculates total price with error handling for each item's price.\"\"\"\n", " total_price = 0\n", " unique_orders = set(customer_orders)\n", " \n", @@ -154,8 +180,44 @@ " break\n", " except ValueError:\n", " print(\"Error: Please enter a valid numeric value for the price.\")\n", + " \n", " for item in customer_orders:\n", - " total_price += prices[item]" + " total_price += prices[item]\n", + " \n", + " return total_price\n", + "\n", + "def main():\n", + " products_list = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", + " try:\n", + " inventory = initialize_inventory(products_list)\n", + " print(f\"\\nInitial Inventory: {inventory}\")\n", + "\n", + " customer_orders = get_customer_orders(inventory)\n", + " \n", + " if not customer_orders:\n", + " print(\"No orders were placed !.\")\n", + " return\n", + " total_unique, percentage = calculate_order_statistics(customer_orders, len(products_list))\n", + " print(f\"\\nOrder Summary: {customer_orders}\")\n", + " print(f\"Total Unique Products Ordered: {total_unique}\")\n", + " print(f\"Percentage of Catalog Ordered: {percentage:.2f}%\")\n", + " \n", + " time.sleep(2)\n", + " update_inventory(customer_orders, inventory)\n", + " \n", + " total_price = calculate_total_price(customer_orders)\n", + " print(f\"\\nFinal Transaction Total: ${total_price:.2f}\")\n", + " \n", + " print(\"\\nFinal Inventory Status:\")\n", + " for product, quantity in inventory.items():\n", + " print(f\"- {product.capitalize()}: {quantity}\")\n", + "\n", + " except Exception as e:\n", + " print(f\"A critical system error occurred: {e}\")\n", + "\n", + "if __name__ == \"__main__\":\n", + " main()" ] } ], From 547ef6573a235221c47f4b27ada4373ae477f527 Mon Sep 17 00:00:00 2001 From: Abanoub Barayo <62501003+abanoubdev@users.noreply.github.com> Date: Fri, 3 Apr 2026 11:27:33 +0200 Subject: [PATCH 4/6] Solved Lab --- lab-python-error-handling.ipynb | 46 ++++++++++++++++++--------------- 1 file changed, 25 insertions(+), 21 deletions(-) diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index 65a9ba8..14b564f 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -75,7 +75,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": null, "id": "1436ad04", "metadata": {}, "outputs": [ @@ -124,28 +124,32 @@ " return inventory\n", "\n", "def get_customer_orders(inventory):\n", - " customer_orders = []\n", - " while True:\n", - " try:\n", - " num_orders = int(input(\"\\nHow many items would you like to order today? \"))\n", - " if num_orders < 0:\n", - " print(\"Error: You cannot order a negative number of items.\")\n", - " continue\n", - " break\n", - " except ValueError:\n", - " print(\"Error: Please enter a valid number for the quantity of orders.\")\n", - "\n", - " for i in range(num_orders):\n", + " customer_orders = set()\n", + " \n", + " try:\n", " while True:\n", - " order = input(f\"Enter item #{i+1} (Available: {', '.join(inventory.keys())}): \").strip().lower()\n", - " \n", - " if order not in inventory:\n", - " print(f\"Error: Sorry, '{order}' is not in our product list.\")\n", - " elif inventory[order] <= 0:\n", - " print(f\"Error: Sorry, '{order}' is currently out of stock!\")\n", - " else:\n", - " customer_orders.append(order)\n", + " try:\n", + " num_orders = int(input(\"\\nHow many different products would you like to order? \"))\n", + " if num_orders < 0:\n", + " print(\"Error: Number of orders cannot be negative.\")\n", + " continue\n", " break\n", + " except ValueError:\n", + " print(\"Error: Please enter a valid number for the quantity of orders.\")\n", + "\n", + " for i in range(num_orders):\n", + " while True:\n", + " order = input(f\"Enter the name of product {i+1}: \").strip().lower()\n", + " \n", + " if order not in inventory:\n", + " print(f\"Error: Sorry, '{order}' is not in our product list. Available: {', '.join(inventory.keys())}\")\n", + " elif inventory[order] <= 0:\n", + " print(f\"Error: Sorry, '{order}' is currently out of stock.\")\n", + " else:\n", + " customer_orders.add(order)\n", + " break\n", + " except Exception as e:\n", + " print(f\"An error occurred while processing orders: {e}\")\n", " \n", " return customer_orders\n", "\n", From f0c0aeba4e0a00c8a16e2ffcccf90c0be04c8539 Mon Sep 17 00:00:00 2001 From: Abanoub Barayo <62501003+abanoubdev@users.noreply.github.com> Date: Fri, 3 Apr 2026 11:33:54 +0200 Subject: [PATCH 5/6] Solved Lab --- lab-python-error-handling.ipynb | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index 14b564f..5a78eae 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -75,7 +75,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "id": "1436ad04", "metadata": {}, "outputs": [ @@ -83,25 +83,29 @@ "name": "stdout", "output_type": "stream", "text": [ + "Error: Please enter a valid whole number for the count.\n", "Error: Inventory count cannot be negative. Please try again.\n", "Error: Inventory count cannot be negative. Please try again.\n", "\n", - "Initial Inventory: {'t-shirt': 88, 'mug': 22, 'hat': 33, 'book': 44, 'keychain': 90}\n", + "Initial Inventory: {'t-shirt': 34, 'mug': 22, 'hat': 90, 'book': 33, 'keychain': 44}\n", + "Error: Sorry, 'hafdfd' is not in our product list. Available: t-shirt, mug, hat, book, keychain\n", + "Error: Sorry, 'fefoe' is not in our product list. Available: t-shirt, mug, hat, book, keychain\n", "\n", - "Order Summary: ['mug', 't-shirt', 'hat']\n", - "Total Unique Products Ordered: 3\n", - "Percentage of Catalog Ordered: 60.00%\n", + "Order Summary: {'hat', 'book', 'mug', 'keychain'}\n", + "Total Unique Products Ordered: 4\n", + "Percentage of Catalog Ordered: 80.00%\n", "\n", "Processing Orders...\n", + "Error: Please enter a valid numeric value for the price.\n", "\n", - "Final Transaction Total: $136.00\n", + "Final Transaction Total: $144.00\n", "\n", "Final Inventory Status:\n", - "- T-shirt: 87\n", + "- T-shirt: 34\n", "- Mug: 21\n", - "- Hat: 32\n", - "- Book: 44\n", - "- Keychain: 90\n" + "- Hat: 89\n", + "- Book: 32\n", + "- Keychain: 43\n" ] } ], @@ -125,7 +129,7 @@ "\n", "def get_customer_orders(inventory):\n", " customer_orders = set()\n", - " \n", + "\n", " try:\n", " while True:\n", " try:\n", @@ -162,13 +166,11 @@ " print(f\"Alert: {item} is now out of stock!\")\n", "\n", "def calculate_order_statistics(customer_orders, total_products_count):\n", - " \"\"\"Calculates basic stats about the order.\"\"\"\n", " unique_products_ordered = len(set(customer_orders))\n", " percentage_ordered = (unique_products_ordered / total_products_count) * 100\n", " return unique_products_ordered, percentage_ordered\n", "\n", "def calculate_total_price(customer_orders):\n", - " \"\"\"Calculates total price with error handling for each item's price.\"\"\"\n", " total_price = 0\n", " unique_orders = set(customer_orders)\n", " \n", From 742f8d155fd0943dc39a3c7d89d334842d7cc59b Mon Sep 17 00:00:00 2001 From: Abanoub Barayo <62501003+abanoubdev@users.noreply.github.com> Date: Fri, 3 Apr 2026 11:37:03 +0200 Subject: [PATCH 6/6] Solved Lab --- lab-python-error-handling.ipynb | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index 5a78eae..00f474a 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": null, "id": "1436ad04", "metadata": {}, "outputs": [ @@ -144,14 +144,17 @@ " for i in range(num_orders):\n", " while True:\n", " order = input(f\"Enter the name of product {i+1}: \").strip().lower()\n", - " \n", - " if order not in inventory:\n", - " print(f\"Error: Sorry, '{order}' is not in our product list. Available: {', '.join(inventory.keys())}\")\n", - " elif inventory[order] <= 0:\n", - " print(f\"Error: Sorry, '{order}' is currently out of stock.\")\n", - " else:\n", - " customer_orders.add(order)\n", + " try:\n", + " if order not in inventory:\n", + " print(f\"Error: Sorry, '{order}' is not in our product list. Available: {', '.join(inventory.keys())}\")\n", + " elif inventory[order] <= 0:\n", + " print(f\"Error: Sorry, '{order}' is currently out of stock.\")\n", + " else:\n", + " customer_orders.add(order)\n", " break\n", + " except ValueError:\n", + " print(\"Error: Please enter a valid number for the quantity of orders.\")\n", + " \n", " except Exception as e:\n", " print(f\"An error occurred while processing orders: {e}\")\n", " \n",