From 6b576f0e66db4bdcfbde8df88e91af6b9ab5d5c2 Mon Sep 17 00:00:00 2001 From: shirleyamponsah5-maker Date: Thu, 21 May 2026 02:36:06 +0200 Subject: [PATCH] Add files via upload --- lab-python-error-handling.ipynb | 260 ++++++++++++++++++++++++++------ 1 file changed, 210 insertions(+), 50 deletions(-) diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index f4c6ef6..c220545 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -1,28 +1,22 @@ { "cells": [ { - "cell_type": "markdown", - "id": "25d7736c-ba17-4aff-b6bb-66eba20fbf4e", + "cell_type": "code", + "execution_count": 1, + "id": "0065f99d-7e92-4057-a3ee-d1bdfa906913", "metadata": {}, + "outputs": [], "source": [ - "# Lab | Error Handling" + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]" ] }, { - "cell_type": "markdown", - "id": "bc99b386-7508-47a0-bcdb-d969deaf6c8b", + "cell_type": "code", + "execution_count": 2, + "id": "2d7bcb94-c866-4638-b788-b95bd4f81025", "metadata": {}, + "outputs": [], "source": [ - "## Exercise: Error Handling for Managing Customer Orders\n", - "\n", - "The implementation of your code for managing customer orders assumes that the user will always enter a valid input. \n", - "\n", - "For example, we could modify the `initialize_inventory` function to include error handling.\n", - " - If the user enters an invalid quantity (e.g., a negative value or a non-numeric value), display an error message and ask them to re-enter the quantity for that product.\n", - " - Use a try-except block to handle the error and continue prompting the user until a valid quantity is entered.\n", - "\n", - "```python\n", - "# Step 1: Define the function for initializing the inventory with error handling\n", "def initialize_inventory(products):\n", " inventory = {}\n", " for product in products:\n", @@ -36,49 +30,215 @@ " except ValueError as error:\n", " print(f\"Error: {error}\")\n", " inventory[product] = quantity\n", - " return inventory\n", - "\n", - "# Or, in another way:\n", - "\n", - "def initialize_inventory(products):\n", - " inventory = {}\n", - " for product in products:\n", - " valid_input = False\n", - " while not valid_input:\n", + " return inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "b1dd0f80-694e-4ba2-b983-9ce649ecfc01", + "metadata": {}, + "outputs": [], + "source": [ + "def get_customer_orders(inventory):\n", + " print(\"\\n--- Customer Orders ---\")\n", + " while True:\n", + " try:\n", + " num_orders = int(input(\"Enter the number of customer orders: \"))\n", + " if num_orders < 0:\n", + " print(\"Number of orders cannot be negative. Please enter a valid number.\")\n", + " else:\n", + " break\n", + " except ValueError:\n", + " print(\"Invalid input. Please enter a numeric value for the number of orders.\")\n", + " customer_orders = set()\n", + " for _ in range(num_orders):\n", + " valid_product = False\n", + " while not valid_product:\n", + " product_name = input(\"Enter the name of a product that a customer wants to order: \").lower()\n", + " if product_name not in inventory:\n", + " print(\"Invalid product name. This product is not in the inventory.\")\n", + " elif inventory[product_name] <= 0:\n", + " print(f\"No stock available for {product_name}. Please choose another product.\")\n", + " else:\n", + " customer_orders.add(product_name)\n", + " valid_product = True\n", + " return customer_orders" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "ef02730b-e374-4e3f-8264-f00bea4efce3", + "metadata": {}, + "outputs": [], + "source": [ + "def update_inventory(customer_orders, inventory):\n", + " for product in customer_orders:\n", + " if product in inventory and inventory[product] > 0:\n", + " inventory[product] -= 1\n", + " inventory = {product: qty for product, qty in inventory.items() if qty > 0}\n", + " return inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "01a3ae03-3b72-4696-ae6b-fe25cc3d9d89", + "metadata": {}, + "outputs": [], + "source": [ + "def calculate_order_statistics(customer_orders, products):\n", + " total = len(customer_orders)\n", + " percentage = (total / len(products)) * 100 if products else 0\n", + " return total, percentage" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "a203c9a5-001f-4e48-9992-5d2aae58adaa", + "metadata": {}, + "outputs": [], + "source": [ + "def print_order_statistics(order_statistics):\n", + " total, percentage = order_statistics\n", + " print(\"\\nOrder Statistics:\")\n", + " print(f\"Total Products Ordered: {total}\")\n", + " print(f\"Percentage of Unique Products Ordered: {percentage}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "461347b5-c77f-4e65-ac54-9f68948a12ec", + "metadata": {}, + "outputs": [], + "source": [ + "def print_updated_inventory(inventory):\n", + " print(\"\\nUpdated Inventory:\")\n", + " [print(f\"{product}: {qty}\") for product, qty in inventory.items()]" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "c42da3d3-1ae1-42f1-aa9d-20ec4cc8cab5", + "metadata": {}, + "outputs": [], + "source": [ + "def calculate_total_price(customer_orders):\n", + " print(\"\\n--- Calculate Total Price ---\")\n", + " total_price = 0.0\n", + " for product in customer_orders:\n", + " valid_price = False\n", + " while not valid_price:\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", + " price = float(input(f\"Enter the price of {product}: \"))\n", + " if price < 0:\n", + " print(\"Price cannot be negative. Please enter a valid price.\")\n", " else:\n", - " print(\"Quantity cannot be negative. Please enter a valid quantity.\")\n", + " total_price += price\n", + " valid_price = True\n", " except ValueError:\n", - " print(\"Invalid input. Please enter a valid quantity.\")\n", - " return inventory\n", - "```\n", - "\n", - "Let's enhance your code by implementing error handling to handle invalid inputs.\n", - "\n", - "Follow the steps below to complete the exercise:\n", - "\n", - "2. Modify the `calculate_total_price` function to include error handling.\n", - " - If the user enters an invalid price (e.g., a negative value or a non-numeric value), display an error message and ask them to re-enter the price for that product.\n", - " - Use a try-except block to handle the error and continue prompting the user until a valid price is entered.\n", - "\n", - "3. Modify the `get_customer_orders` function to include error handling.\n", - " - If the user enters an invalid number of orders (e.g., a negative value or a non-numeric value), display an error message and ask them to re-enter the number of orders.\n", - " - If the user enters an invalid product name (e.g., a product name that is not in the inventory), or that doesn't have stock available, display an error message and ask them to re-enter the product name. *Hint: you will need to pass inventory as a parameter*\n", - " - Use a try-except block to handle the error and continue prompting the user until a valid product name is entered.\n", - "\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" + " print(\"Invalid input. Please enter a numeric value for the price.\")\n", + " return total_price" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "5c20ef0e-ace2-44f3-bc83-23b002eb91c7", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of t-shirts available: 5\n", + "Enter the quantity of mugs available: 4\n", + "Enter the quantity of hats available: 3\n", + "Enter the quantity of books available: 2\n", + "Enter the quantity of keychains available: 1\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "--- Customer Orders ---\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the number of customer orders: 2\n", + "Enter the name of a product that a customer wants to order: hat\n", + "Enter the name of a product that a customer wants to order: keychain\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Order Statistics:\n", + "Total Products Ordered: 2\n", + "Percentage of Unique Products Ordered: 40.0\n", + "\n", + "Updated Inventory:\n", + "t-shirt: 5\n", + "mug: 4\n", + "hat: 2\n", + "book: 2\n", + "\n", + "--- Calculate Total Price ---\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the price of hat: 5\n", + "Enter the price of keychain: 10\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Total Price: 15.0\n" + ] + } + ], + "source": [ + "inventory = initialize_inventory(products)\n", + "customer_orders = get_customer_orders(inventory)\n", + "order_stats = calculate_order_statistics(customer_orders, products)\n", + "print_order_statistics(order_stats)\n", + "inventory = update_inventory(customer_orders, inventory)\n", + "print_updated_inventory(inventory)\n", + "total_price = calculate_total_price(customer_orders)\n", + "print(f\"\\nTotal Price: {total_price}\")" ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "adcf3b3a-6d42-49c8-a03d-31f771e3abda", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python [conda env:base] *", "language": "python", - "name": "python3" + "name": "conda-base-py" }, "language_info": { "codemirror_mode": { @@ -90,7 +250,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.9" } }, "nbformat": 4,