From 2f0fd67b0170b0a5e17e56310a7ffefbcab18f22 Mon Sep 17 00:00:00 2001 From: Hina Haq Date: Fri, 3 Apr 2026 15:56:38 +0200 Subject: [PATCH 1/3] completed error handling lab --- lab-python-error-handling.ipynb | 132 +++++++++++++++++++++++++++++++- 1 file changed, 130 insertions(+), 2 deletions(-) diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index f4c6ef6..ab22717 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -72,11 +72,139 @@ "\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": 13, + "id": "ebabdc77", + "metadata": {}, + "outputs": [], + "source": [ + "# Initialize inventory with error handling\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(\"Quantity cannot be negative. Please enter a valid quantity.\")\n", + " except ValueError:\n", + " print(\"Invalid input. Please enter a valid quantity.\")\n", + " return inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "4c48d1e0", + "metadata": {}, + "outputs": [], + "source": [ + "# Calculate total price with error handling\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", + " total_price += price\n", + " valid_input = True\n", + " else:\n", + " print(\"Error: Invalid price! Please enter a non-negative value.\")\n", + " except ValueError as error:\n", + " print(f\"Error: {error}\")\n", + " return total_price" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "id": "85a56ef4", + "metadata": {}, + "outputs": [], + "source": [ + "# Get customer orders with error handling\n", + "def get_customer_orders(inventory):\n", + " customer_orders = set()\n", + "\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", + " raise ValueError(\"Number of orders cannot be negative.\")\n", + " valid_input = True\n", + " except ValueError as error:\n", + " print(f\"Error: {error}\")\n", + "\n", + " for _ in range(num_orders):\n", + " valid_product = False\n", + " while not valid_product:\n", + " try:\n", + " product = input(\"Enter the name of a product that a customer wants to order: \")\n", + " if product not in inventory:\n", + " raise ValueError(f\"'{product}' is not in the inventory.\")\n", + " if inventory[product] == 0:\n", + " raise ValueError(f\"'{product}' is out of stock.\")\n", + " customer_orders.add(product)\n", + " valid_product = True\n", + " except ValueError as error:\n", + " print(f\"Error: {error}\")\n", + "\n", + " return customer_orders" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "id": "822b7688", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Invalid input. Please enter a valid quantity.\n", + "Quantity cannot be negative. Please enter a valid quantity.\n", + "Invalid input. Please enter a valid quantity.\n", + "Error: 'icecream' is not in the inventory.\n", + "Error: '-4' is not in the inventory.\n", + "Error: '4' is not in the inventory.\n", + "Error: 'hatmat' is not in the inventory.\n", + "Error: 'escape' is not in the inventory.\n", + "Error: 'escape' is not in the inventory.\n", + "Error: '8' is not in the inventory.\n", + "Error: '9' is not in the inventory.\n", + "Error: '9' is not in the inventory.\n", + "\n", + "Customer Orders: {'t-shirt', 'book', 'mug', 'hat'}\n", + "Total Price: 940.0\n" + ] + } + ], + "source": [ + "# Test the program\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", + "inventory = initialize_inventory(products)\n", + "customer_orders = get_customer_orders(inventory)\n", + "total_price = calculate_total_price(customer_orders)\n", + "\n", + "print(\"\\nCustomer Orders:\", customer_orders)\n", + "print(f\"Total Price: {total_price}\")" + ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, @@ -90,7 +218,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.9" } }, "nbformat": 4, From 03383ba8496a04016d11365d04bfad94b2611f7f Mon Sep 17 00:00:00 2001 From: Hina Haq Date: Fri, 3 Apr 2026 16:41:44 +0200 Subject: [PATCH 2/3] completed error handling lab --- lab-python-error-handling.ipynb | 129 ++++++++++++++++++-------------- 1 file changed, 71 insertions(+), 58 deletions(-) diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index ab22717..138035c 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -75,130 +75,143 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": null, "id": "ebabdc77", "metadata": {}, "outputs": [], "source": [ - "# Initialize inventory with error handling\n", + "# Exercise: Error Handling for Managing Customer Orders\n", + "# Def function for initializing the inventory with error handling\n", "def initialize_inventory(products):\n", " inventory = {}\n", + "\n", " for product in products:\n", - " valid_input = False\n", - " while not valid_input:\n", + " valid_quantity = False\n", + "\n", + " while not valid_quantity:\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", + "\n", + " if quantity < 0:\n", + " print(\"Error: Quantity cannot be negative. Please enter a valid quantity.\")\n", " else:\n", - " print(\"Quantity cannot be negative. Please enter a valid quantity.\")\n", + " inventory[product] = quantity\n", + " valid_quantity = True\n", + "\n", " except ValueError:\n", - " print(\"Invalid input. Please enter a valid quantity.\")\n", + " print(\"Error: Invalid input. Please enter a valid quantity.\")\n", + "\n", " return inventory" ] }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 8, "id": "4c48d1e0", "metadata": {}, "outputs": [], "source": [ "# Calculate total price with error handling\n", + "\n", "def calculate_total_price(customer_orders):\n", " total_price = 0\n", + "\n", " for product in customer_orders:\n", - " valid_input = False\n", - " while not valid_input:\n", + " valid_price = False\n", + "\n", + " while not valid_price:\n", " try:\n", " price = float(input(f\"Enter the price of {product}: \"))\n", - " if price >= 0:\n", - " total_price += price\n", - " valid_input = True\n", - " else:\n", + "\n", + " if price < 0:\n", " print(\"Error: Invalid price! Please enter a non-negative value.\")\n", - " except ValueError as error:\n", - " print(f\"Error: {error}\")\n", + " else:\n", + " total_price += price\n", + " valid_price = True\n", + "\n", + " except ValueError:\n", + " print(\"Error: Invalid input. Please enter a numeric value.\")\n", + "\n", " return total_price" ] }, { "cell_type": "code", - "execution_count": 16, - "id": "85a56ef4", + "execution_count": null, + "id": "f6eadd95", "metadata": {}, "outputs": [], "source": [ - "# Get customer orders with error handling\n", + "# get_customer_orders function to include error handling\n", + "\n", "def get_customer_orders(inventory):\n", " customer_orders = set()\n", "\n", - " valid_input = False\n", - " while not valid_input:\n", + " valid_number = False\n", + " while not valid_number:\n", " try:\n", - " num_orders = int(input(\"Enter the number of customer orders: \"))\n", - " if num_orders < 0:\n", - " raise ValueError(\"Number of orders cannot be negative.\")\n", - " valid_input = True\n", - " except ValueError as error:\n", - " print(f\"Error: {error}\")\n", - "\n", - " for _ in range(num_orders):\n", + " number_of_orders = int(input(\"Enter the number of customer orders: \"))\n", + "\n", + " if number_of_orders < 0:\n", + " print(\"Error: Number of orders cannot be negative.\")\n", + " else:\n", + " valid_number = True\n", + "\n", + " except ValueError:\n", + " print(\"Error: Invalid input. Please enter a whole number.\")\n", + "\n", + " for i in range(number_of_orders):\n", " valid_product = False\n", + "\n", " while not valid_product:\n", - " try:\n", - " product = input(\"Enter the name of a product that a customer wants to order: \")\n", - " if product not in inventory:\n", - " raise ValueError(f\"'{product}' is not in the inventory.\")\n", - " if inventory[product] == 0:\n", - " raise ValueError(f\"'{product}' is out of stock.\")\n", + " product = input(f\"Enter product {i+1}: \").strip().lower()\n", + "\n", + " if product not in inventory:\n", + " print(f\"Error: '{product}' is not in the inventory.\")\n", + " elif inventory[product] <= 0:\n", + " print(f\"Error: '{product}' is out of stock.\")\n", + " else:\n", " customer_orders.add(product)\n", + " inventory[product] -= 1\n", " valid_product = True\n", - " except ValueError as error:\n", - " print(f\"Error: {error}\")\n", "\n", " return customer_orders" ] }, { "cell_type": "code", - "execution_count": 17, - "id": "822b7688", + "execution_count": null, + "id": "85a56ef4", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "Invalid input. Please enter a valid quantity.\n", - "Quantity cannot be negative. Please enter a valid quantity.\n", - "Invalid input. Please enter a valid quantity.\n", - "Error: 'icecream' is not in the inventory.\n", - "Error: '-4' is not in the inventory.\n", - "Error: '4' is not in the inventory.\n", - "Error: 'hatmat' is not in the inventory.\n", + "Error: Quantity cannot be negative. Please enter a valid quantity.\n", + "Error: Invalid input. Please enter a valid quantity.\n", + "Error: Invalid input. Please enter a valid quantity.\n", + "Error: Invalid input. Please enter a valid quantity.\n", + "Error: Invalid input. Please enter a whole number.\n", "Error: 'escape' is not in the inventory.\n", - "Error: 'escape' is not in the inventory.\n", - "Error: '8' is not in the inventory.\n", - "Error: '9' is not in the inventory.\n", - "Error: '9' is not in the inventory.\n", - "\n", - "Customer Orders: {'t-shirt', 'book', 'mug', 'hat'}\n", - "Total Price: 940.0\n" + "Inventory: {'t-shirt': 77, 'mug': 86, 'hat': 96, 'book': 99, 'keychain': 76}\n", + "Customer Orders: {'mug', 't-shirt', 'hat', 'keychain', 'book'}\n", + "Total Price: 418.0\n" ] } ], "source": [ - "# Test the program\n", + "# Test the program with invalid inputs\n", + "\n", "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", "\n", "inventory = initialize_inventory(products)\n", "customer_orders = get_customer_orders(inventory)\n", "total_price = calculate_total_price(customer_orders)\n", "\n", - "print(\"\\nCustomer Orders:\", customer_orders)\n", - "print(f\"Total Price: {total_price}\")" + "print(\"Inventory:\", inventory)\n", + "print(\"Customer Orders:\", customer_orders)\n", + "print(\"Total Price:\", total_price)" ] } ], From c79b98b577b535029861828c8add833aa0704ff3 Mon Sep 17 00:00:00 2001 From: Hina Haq Date: Fri, 3 Apr 2026 17:01:14 +0200 Subject: [PATCH 3/3] add test with invalid inputs --- lab-python-error-handling.ipynb | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index 138035c..1fbfb18 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -138,12 +138,11 @@ { "cell_type": "code", "execution_count": null, - "id": "f6eadd95", + "id": "9686b539", "metadata": {}, "outputs": [], "source": [ - "# get_customer_orders function to include error handling\n", - "\n", + "# Modify the get_customer_orders function to include error handling\n", "def get_customer_orders(inventory):\n", " customer_orders = set()\n", "\n", @@ -180,7 +179,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 12, "id": "85a56ef4", "metadata": {}, "outputs": [ @@ -188,15 +187,14 @@ "name": "stdout", "output_type": "stream", "text": [ - "Error: Quantity cannot be negative. Please enter a valid quantity.\n", - "Error: Invalid input. Please enter a valid quantity.\n", "Error: Invalid input. Please enter a valid quantity.\n", + "Error: Quantity cannot be negative. Please enter a valid quantity.\n", "Error: Invalid input. Please enter a valid quantity.\n", - "Error: Invalid input. Please enter a whole number.\n", - "Error: 'escape' is not in the inventory.\n", - "Inventory: {'t-shirt': 77, 'mug': 86, 'hat': 96, 'book': 99, 'keychain': 76}\n", - "Customer Orders: {'mug', 't-shirt', 'hat', 'keychain', 'book'}\n", - "Total Price: 418.0\n" + "Error: 'bokk' is not in the inventory.\n", + "Error: Invalid price! Please enter a non-negative value.\n", + "Inventory: {'t-shirt': 66, 'mug': 64, 'hat': 87, 'book': 99, 'keychain': 77}\n", + "Customer Orders: {'hat', 'mug', 'book', 'keychain'}\n", + "Total Price: 102.65\n" ] } ],