From 525c58f942829f2be9776ce340f87b211d89f167 Mon Sep 17 00:00:00 2001 From: michaelgrasshopper Date: Sun, 17 May 2026 16:06:58 +0200 Subject: [PATCH 1/5] lab-python-error-handling-lab-5 --- lab-python-error-handling.ipynb | 103 ++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index f4c6ef6..e74cc08 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -72,6 +72,109 @@ "\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": "d152b257", + "metadata": {}, + "outputs": [], + "source": [ + "# 1.\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", + "# 2.\n", + "def initialize_inventory(products):\n", + " inventory = {}\n", + " for product in products:\n", + " valid_input = False\n", + " while not valid_input:\n", + " try:\n", + " quantity = int(input(f\"Enter the quantity for {product}: \"))\n", + " if quantity < 0:\n", + " inventory[product] = quantity\n", + " valid_input = True\n", + " else:\n", + " print(\"Quantity cannot be negative. Please enter a valid quantity.\")\n", + " except ValueError:\n", + " print(\"Invalid input. Please enter a numeric value for the quantity.\")\n", + " return inventory\n", + "\n", + "\n", + "# 3.\n", + "def get_customer_orders(inventory):\n", + " valid_input = False\n", + " while not valid_input:\n", + " try:\n", + " num_orders = int(input(\"Enter the number of products the customer wants to order: \"))\n", + " if num_orders < 0:\n", + " print(\"Number of products cannot be negative. Please enter a valid number.\")\n", + " else:\n", + " valid_input = True\n", + " except ValueError:\n", + " print(\"Invalid input. Please enter a numeric value for the number of products.\")\n", + " \n", + "\n", + "# 4.\n", + "customer_orders = set[]\n", + "for i in range(num_orders):\n", + " valid_product = False\n", + " while not valid_product:\n", + " try:\n", + " order = input(\"Enter the name of a product that a customer wants to order: \").strip().lower()\n", + " if order not in inventory:\n", + " raise ValueError(f\"'{order}' is not a valid product. Choose from: {', '.join(inventory.keys())}\")\n", + " elif inventory[order] == 0:\n", + " raise ValueError(f\"Sorry, '{order}' is out of stock. Please choose another product.\")\n", + " else:\n", + " customer_orders.add(order)\n", + " valid_product = True\n", + " except ValueError as error:\n", + " print(f\"Error: {error}\")\n", + "\n", + "return customer_orders\n", + "\n", + "# 5.\n", + "def inventory_update(customer_orders, inventory):\n", + " inventory = {product: inventory[product] - 1 for product in customer_orders}\n", + " inventory = {product: quantity for product, quantity in inventory.items() if quantity > 0}\n", + "\n", + "# 6.\n", + "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)\n", + "\n", + "# 7.\n", + "def print_order_statistics(order_statistics):\n", + " print(f\"\"\"\n", + "Order Statistics:\n", + "Total Products Ordered: {order_statistics[0]}\n", + "Percentage of Unique Products Ordered: {order_statistics[1]}\n", + " \"\"\")\n", + "\n", + "# 8.\n", + "def print_updated_inventory(inventory):\n", + " print(\"Updated Inventory:\")\n", + " [print(f\"{product}: {quantity}\") for product, quantity in inventory.items()]\n", + "\n", + "# Main function to run the program\n", + "# step 1.\n", + "inventory = initialize_inventory(products)\n", + "# step 2.\n", + "customer_orders = get_customer_orders()\n", + "# step 3.\n", + "order_statistics = calculate_order_statistics(customer_orders, products)\n", + "# step 4.\n", + "print_order_statistics(order_statistics)\n", + "# step 5.\n", + "inventory_update(customer_orders, inventory)\n", + "# step 6.\n", + "print_updated_inventory(inventory)\n", + "# step 7.\n", + "total_price = calculate_total_price(customer_orders)\n", + "print(f\"Total Price: {total_price}\")" + ] } ], "metadata": { From 01b6647c4394096c0e984c47fe636b7493983edc Mon Sep 17 00:00:00 2001 From: michaelgrasshopper Date: Sun, 17 May 2026 16:15:31 +0200 Subject: [PATCH 2/5] lab-python-error-handling-lab-5 --- lab-python-error-handling.ipynb | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index e74cc08..8279d35 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -80,10 +80,9 @@ "metadata": {}, "outputs": [], "source": [ - "# 1.\n", "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", "\n", - "# 2.\n", + "# 1.\n", "def initialize_inventory(products):\n", " inventory = {}\n", " for product in products:\n", @@ -101,7 +100,7 @@ " return inventory\n", "\n", "\n", - "# 3.\n", + "# 2.\n", "def get_customer_orders(inventory):\n", " valid_input = False\n", " while not valid_input:\n", @@ -115,7 +114,7 @@ " print(\"Invalid input. Please enter a numeric value for the number of products.\")\n", " \n", "\n", - "# 4.\n", + "# 3.\n", "customer_orders = set[]\n", "for i in range(num_orders):\n", " valid_product = False\n", @@ -134,18 +133,18 @@ "\n", "return customer_orders\n", "\n", - "# 5.\n", + "# 4.\n", "def inventory_update(customer_orders, inventory):\n", " inventory = {product: inventory[product] - 1 for product in customer_orders}\n", " inventory = {product: quantity for product, quantity in inventory.items() if quantity > 0}\n", "\n", - "# 6.\n", + "# 5.\n", "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)\n", "\n", - "# 7.\n", + "# 6.\n", "def print_order_statistics(order_statistics):\n", " print(f\"\"\"\n", "Order Statistics:\n", @@ -153,7 +152,7 @@ "Percentage of Unique Products Ordered: {order_statistics[1]}\n", " \"\"\")\n", "\n", - "# 8.\n", + "# 7.\n", "def print_updated_inventory(inventory):\n", " print(\"Updated Inventory:\")\n", " [print(f\"{product}: {quantity}\") for product, quantity in inventory.items()]\n", From 21517b1fc41aaf250c5b1041076e47e080da0f73 Mon Sep 17 00:00:00 2001 From: michaelgrasshopper Date: Sun, 17 May 2026 16:21:49 +0200 Subject: [PATCH 3/5] lab-python-error-handling-lab-5 --- lab-python-error-handling.ipynb | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index 8279d35..53137e7 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -158,19 +158,25 @@ " [print(f\"{product}: {quantity}\") for product, quantity in inventory.items()]\n", "\n", "# Main function to run the program\n", - "# step 1.\n", + "# Step 1: Initialize the inventory\n", "inventory = initialize_inventory(products)\n", - "# step 2.\n", - "customer_orders = get_customer_orders()\n", - "# step 3.\n", + "\n", + "# Step 2: Get customer orders (inventory passed for stock validation)\n", + "customer_orders = get_customer_orders(inventory)\n", + "\n", + "# Step 3: Calculate order statistics\n", "order_statistics = calculate_order_statistics(customer_orders, products)\n", - "# step 4.\n", + "\n", + "# Step 4: Print order statistics\n", "print_order_statistics(order_statistics)\n", - "# step 5.\n", - "inventory_update(customer_orders, inventory)\n", - "# step 6.\n", + "\n", + "# Step 5: Update the inventory\n", + "inventory = update_inventory(customer_orders, inventory)\n", + "\n", + "# Step 6: Print the updated inventory\n", "print_updated_inventory(inventory)\n", - "# step 7.\n", + "\n", + "# Step 7: Calculate and print total price\n", "total_price = calculate_total_price(customer_orders)\n", "print(f\"Total Price: {total_price}\")" ] From 51a3175ec82712fa9203153743588fdcafe39f2a Mon Sep 17 00:00:00 2001 From: michaelgrasshopper Date: Sun, 17 May 2026 16:35:26 +0200 Subject: [PATCH 4/5] lab-python-error-handling-lab-5 --- lab-python-error-handling.ipynb | 101 +++++++++++++++++++++++--------- 1 file changed, 72 insertions(+), 29 deletions(-) diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index 53137e7..bcd2f2e 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -80,47 +80,53 @@ "metadata": {}, "outputs": [], "source": [ + "# Define the products list\n", "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", "\n", - "# 1.\n", + "# ----------------------------------------\n", + "# 1. Initialize Inventory - with error handling\n", + "# ----------------------------------------\n", "def initialize_inventory(products):\n", " inventory = {}\n", " for product in products:\n", " valid_input = False\n", " while not valid_input:\n", " try:\n", - " quantity = int(input(f\"Enter the quantity for {product}: \"))\n", - " if quantity < 0:\n", + " quantity = int(input(f\"Enter the quantity of {product}s available: \"))\n", + " if quantity >= 0:\n", " inventory[product] = quantity\n", " valid_input = True\n", " else:\n", - " print(\"Quantity cannot be negative. Please enter a valid quantity.\")\n", + " print(\"Error: Quantity cannot be negative. Please enter a valid quantity.\")\n", " except ValueError:\n", - " print(\"Invalid input. Please enter a numeric value for the quantity.\")\n", + " print(\"Error: Invalid input. Please enter a numeric value.\")\n", " return inventory\n", "\n", "\n", - "# 2.\n", + "# ----------------------------------------\n", + "# 2. Get Customer Orders - with error handling\n", + "# ----------------------------------------\n", "def get_customer_orders(inventory):\n", + " \n", + " # Validate number of orders\n", " valid_input = False\n", " while not valid_input:\n", " try:\n", - " num_orders = int(input(\"Enter the number of products the customer wants to order: \"))\n", - " if num_orders < 0:\n", - " print(\"Number of products cannot be negative. Please enter a valid number.\")\n", + " num_orders = int(input(\"Enter the number of customer orders: \"))\n", + " if num_orders <= 0:\n", + " print(\"Error: Number of orders must be a positive value.\")\n", " else:\n", " valid_input = True\n", " except ValueError:\n", - " print(\"Invalid input. Please enter a numeric value for the number of products.\")\n", - " \n", - "\n", - "# 3.\n", - "customer_orders = set[]\n", - "for i in range(num_orders):\n", - " valid_product = False\n", - " while not valid_product:\n", - " try:\n", - " order = input(\"Enter the name of a product that a customer wants to order: \").strip().lower()\n", + " print(\"Error: Invalid input. Please enter a numeric value.\")\n", + "\n", + " # Validate each product name and stock availability\n", + " customer_orders = set()\n", + " for _ in range(num_orders):\n", + " valid_product = False\n", + " while not valid_product:\n", + " try:\n", + " order = input(\"Enter the name of a product that a customer wants to order: \").strip().lower()\n", " if order not in inventory:\n", " raise ValueError(f\"'{order}' is not a valid product. Choose from: {', '.join(inventory.keys())}\")\n", " elif inventory[order] == 0:\n", @@ -128,23 +134,53 @@ " else:\n", " customer_orders.add(order)\n", " valid_product = True\n", - " except ValueError as error:\n", - " print(f\"Error: {error}\")\n", + " except ValueError as error:\n", + " print(f\"Error: {error}\")\n", + "\n", + " return customer_orders\n", + "\n", + "\n", + "# ----------------------------------------\n", + "# 3. Calculate Total Price - with error handling\n", + "# ----------------------------------------\n", + "def calculate_total_price(customer_orders):\n", + " total_price = 0\n", + " for product in customer_orders:\n", + " valid_input = False\n", + " while not valid_input:\n", + " try:\n", + " price = float(input(f\"Enter the price of {product}: \"))\n", + " if price < 0:\n", + " print(\"Error: Price cannot be negative. Please enter a valid price.\")\n", + " else:\n", + " total_price += price\n", + " valid_input = True\n", + " except ValueError:\n", + " print(\"Error: Invalid input. Please enter a numeric value.\")\n", + " return total_price\n", "\n", - "return customer_orders\n", "\n", - "# 4.\n", - "def inventory_update(customer_orders, inventory):\n", - " inventory = {product: inventory[product] - 1 for product in customer_orders}\n", + "# ----------------------------------------\n", + "# 4. Update Inventory - using comprehension + remove zero quantity\n", + "# ----------------------------------------\n", + "def update_inventory(customer_orders, inventory):\n", + " inventory = {product: inventory[product] - (1 if product in customer_orders else 0) for product in inventory}\n", " inventory = {product: quantity for product, quantity in inventory.items() if quantity > 0}\n", + " return inventory\n", + "\n", "\n", - "# 5.\n", + "# ----------------------------------------\n", + "# 5. Calculate Order Statistics\n", + "# ----------------------------------------\n", "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)\n", "\n", - "# 6.\n", + "\n", + "# ----------------------------------------\n", + "# 6. Print Order Statistics\n", + "# ----------------------------------------\n", "def print_order_statistics(order_statistics):\n", " print(f\"\"\"\n", "Order Statistics:\n", @@ -152,12 +188,19 @@ "Percentage of Unique Products Ordered: {order_statistics[1]}\n", " \"\"\")\n", "\n", - "# 7.\n", + "\n", + "# ----------------------------------------\n", + "# 7. Print Updated Inventory - using comprehension\n", + "# ----------------------------------------\n", "def print_updated_inventory(inventory):\n", " print(\"Updated Inventory:\")\n", " [print(f\"{product}: {quantity}\") for product, quantity in inventory.items()]\n", "\n", - "# Main function to run the program\n", + "\n", + "# ----------------------------------------\n", + "# 8. Main Program - Call functions in sequence\n", + "# ----------------------------------------\n", + "\n", "# Step 1: Initialize the inventory\n", "inventory = initialize_inventory(products)\n", "\n", From 25f4f4f9d7bd12c99319e007337b6409debe4206 Mon Sep 17 00:00:00 2001 From: michaelgrasshopper Date: Sun, 17 May 2026 16:52:30 +0200 Subject: [PATCH 5/5] lab-python-error-handling-lab-5 --- lab-python-error-handling.ipynb | 106 ++++++-------------------------- 1 file changed, 18 insertions(+), 88 deletions(-) diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index bcd2f2e..40060dc 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -83,104 +83,40 @@ "# Define the products list\n", "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", "\n", - "# ----------------------------------------\n", - "# 1. Initialize Inventory - with error handling\n", - "# ----------------------------------------\n", + "# 1. Initialize Inventory\n", "def initialize_inventory(products):\n", - " inventory = {}\n", - " for product in products:\n", - " valid_input = False\n", - " while not valid_input:\n", - " try:\n", - " quantity = int(input(f\"Enter the quantity of {product}s available: \"))\n", - " if quantity >= 0:\n", - " inventory[product] = quantity\n", - " valid_input = True\n", - " else:\n", - " print(\"Error: Quantity cannot be negative. Please enter a valid quantity.\")\n", - " except ValueError:\n", - " print(\"Error: Invalid input. Please enter a numeric value.\")\n", + " inventory = {product: int(input(f\"Enter the quantity of {product}s available: \")) for product in products}\n", " return inventory\n", "\n", - "\n", - "# ----------------------------------------\n", - "# 2. Get Customer Orders - with error handling\n", - "# ----------------------------------------\n", - "def get_customer_orders(inventory):\n", - " \n", - " # Validate number of orders\n", - " valid_input = False\n", - " while not valid_input:\n", - " try:\n", - " num_orders = int(input(\"Enter the number of customer orders: \"))\n", - " if num_orders <= 0:\n", - " print(\"Error: Number of orders must be a positive value.\")\n", - " else:\n", - " valid_input = True\n", - " except ValueError:\n", - " print(\"Error: Invalid input. Please enter a numeric value.\")\n", - "\n", - " # Validate each product name and stock availability\n", - " customer_orders = set()\n", - " for _ in range(num_orders):\n", - " valid_product = False\n", - " while not valid_product:\n", - " try:\n", - " order = input(\"Enter the name of a product that a customer wants to order: \").strip().lower()\n", - " if order not in inventory:\n", - " raise ValueError(f\"'{order}' is not a valid product. Choose from: {', '.join(inventory.keys())}\")\n", - " elif inventory[order] == 0:\n", - " raise ValueError(f\"Sorry, '{order}' is out of stock. Please choose another product.\")\n", - " else:\n", - " customer_orders.add(order)\n", - " valid_product = True\n", - " except ValueError as error:\n", - " print(f\"Error: {error}\")\n", - "\n", + "# 2. Get Customer Orders\n", + "def get_customer_orders():\n", + " num_orders = int(input(\"Enter the number of customer orders: \"))\n", + " customer_orders = {\n", + " order\n", + " for _ in range(num_orders)\n", + " for order in [input(\"Enter the name of a product that a customer wants to order: \").strip().lower()]\n", + " if order in products or print(f\"Invalid product '{order}'. Please choose from: {', '.join(products)}\") \n", + " }\n", " return customer_orders\n", "\n", - "\n", - "# ----------------------------------------\n", - "# 3. Calculate Total Price - with error handling\n", - "# ----------------------------------------\n", + "# 3. Calculate Total Price\n", "def calculate_total_price(customer_orders):\n", - " total_price = 0\n", - " for product in customer_orders:\n", - " valid_input = False\n", - " while not valid_input:\n", - " try:\n", - " price = float(input(f\"Enter the price of {product}: \"))\n", - " if price < 0:\n", - " print(\"Error: Price cannot be negative. Please enter a valid price.\")\n", - " else:\n", - " total_price += price\n", - " valid_input = True\n", - " except ValueError:\n", - " print(\"Error: Invalid input. Please enter a numeric value.\")\n", + " total_price = sum(float(input(f\"Enter the price of {product}: \")) for product in customer_orders)\n", " return total_price\n", "\n", - "\n", - "# ----------------------------------------\n", - "# 4. Update Inventory - using comprehension + remove zero quantity\n", - "# ----------------------------------------\n", + "# 4. Update Inventory\n", "def update_inventory(customer_orders, inventory):\n", " inventory = {product: inventory[product] - (1 if product in customer_orders else 0) for product in inventory}\n", " inventory = {product: quantity for product, quantity in inventory.items() if quantity > 0}\n", " return inventory\n", "\n", - "\n", - "# ----------------------------------------\n", "# 5. Calculate Order Statistics\n", - "# ----------------------------------------\n", "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)\n", "\n", - "\n", - "# ----------------------------------------\n", "# 6. Print Order Statistics\n", - "# ----------------------------------------\n", "def print_order_statistics(order_statistics):\n", " print(f\"\"\"\n", "Order Statistics:\n", @@ -188,24 +124,18 @@ "Percentage of Unique Products Ordered: {order_statistics[1]}\n", " \"\"\")\n", "\n", - "\n", - "# ----------------------------------------\n", - "# 7. Print Updated Inventory - using comprehension\n", - "# ----------------------------------------\n", + "# 7. Print Updated Inventory\n", "def print_updated_inventory(inventory):\n", " print(\"Updated Inventory:\")\n", " [print(f\"{product}: {quantity}\") for product, quantity in inventory.items()]\n", "\n", - "\n", - "# ----------------------------------------\n", - "# 8. Main Program - Call functions in sequence\n", - "# ----------------------------------------\n", + "# 8. Main Program\n", "\n", "# Step 1: Initialize the inventory\n", "inventory = initialize_inventory(products)\n", "\n", - "# Step 2: Get customer orders (inventory passed for stock validation)\n", - "customer_orders = get_customer_orders(inventory)\n", + "# Step 2: Get customer orders\n", + "customer_orders = get_customer_orders()\n", "\n", "# Step 3: Calculate order statistics\n", "order_statistics = calculate_order_statistics(customer_orders, products)\n",