diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index f4c6ef6..4f8896a 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -72,11 +72,216 @@ "\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": "ed41bf89", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "--- Initialize Inventory ---\n", + "Error: Invalid input! Please enter a valid whole number.\n", + "Error: Invalid input! Please enter a valid whole number.\n", + "Error: Invalid input! Please enter a valid whole number.\n", + "Error: Quantity cannot be negative. Please try again.\n", + "Error: Quantity cannot be negative. Please try again.\n", + "\n", + "--- Get Customer Orders ---\n", + "Error: 'pen' does not exist in our inventory catalog. Please try a different product.\n", + "Error: 'pencil' does not exist in our inventory catalog. Please try a different product.\n", + "Error: 'rubber' does not exist in our inventory catalog. Please try a different product.\n", + "Error: 'notebook' does not exist in our inventory catalog. Please try a different product.\n", + "Error: 'hoodie' does not exist in our inventory catalog. Please try a different product.\n", + "Error: 'letter' does not exist in our inventory catalog. Please try a different product.\n", + "Error: 'laptop' does not exist in our inventory catalog. Please try a different product.\n", + "Error: 'computer' does not exist in our inventory catalog. Please try a different product.\n", + "Error: 'letter' does not exist in our inventory catalog. Please try a different product.\n", + "Error: 'headphone' does not exist in our inventory catalog. Please try a different product.\n", + "Error: 'bag' does not exist in our inventory catalog. Please try a different product.\n", + "Error: 'tab' does not exist in our inventory catalog. Please try a different product.\n", + "Error: 'spoon' does not exist in our inventory catalog. Please try a different product.\n", + "Error: 'jbelt' does not exist in our inventory catalog. Please try a different product.\n", + "Error: 'belt' does not exist in our inventory catalog. Please try a different product.\n", + "Error: 'cable' does not exist in our inventory catalog. Please try a different product.\n", + "Error: 'toy' does not exist in our inventory catalog. Please try a different product.\n", + "Error: 'router' does not exist in our inventory catalog. Please try a different product.\n", + "Error: 'chair' does not exist in our inventory catalog. Please try a different product.\n", + "Error: 'clock' does not exist in our inventory catalog. Please try a different product.\n", + "Error: 'mouse' does not exist in our inventory catalog. Please try a different product.\n", + "Error: 'bed' does not exist in our inventory catalog. Please try a different product.\n", + "Error: 'mat' does not exist in our inventory catalog. Please try a different product.\n", + "Error: 'violin' does not exist in our inventory catalog. Please try a different product.\n", + "Error: 'computer' does not exist in our inventory catalog. Please try a different product.\n", + "Error: 'tape' does not exist in our inventory catalog. Please try a different product.\n", + "Error: '' does not exist in our inventory catalog. Please try a different product.\n" + ] + } + ], + "source": [ + "from functools import reduce\n", + "\n", + "def initialize_inventory(products):\n", + " \"\"\"1. Initializes the inventory dictionary with robust numeric validation loops.\"\"\"\n", + " inventory = {}\n", + " print(\"\\n--- Initialize 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: \").strip())\n", + " if quantity >= 0:\n", + " inventory[product] = quantity\n", + " valid_input = True\n", + " else:\n", + " print(\"Error: Quantity cannot be negative. Please try again.\")\n", + " except ValueError:\n", + " print(\"Error: Invalid input! Please enter a valid whole number.\")\n", + " return inventory\n", + "\n", + "\n", + "def get_customer_orders(inventory):\n", + " \"\"\"3. Gathers orders with error handling for negative counts, missing items, and out-of-stock items.\"\"\"\n", + " print(\"\\n--- Get Customer Orders ---\")\n", + " \n", + " # Part A: Validate the total number of orders requested\n", + " while True:\n", + " try:\n", + " num_orders = int(input(\"Enter the number of customer orders: \").strip())\n", + " if num_orders < 0:\n", + " print(\"Error: Number of orders cannot be negative. Please try again.\")\n", + " continue\n", + " break\n", + " except ValueError:\n", + " print(\"Error: Invalid input! Please enter a valid whole number.\")\n", + "\n", + " # Create a quick lowercase mapping to handle case-insensitive entries safely\n", + " inventory_lowercase = {product.lower(): product for product in inventory}\n", + " customer_orders = set()\n", + " \n", + " # Part B: Gather and validate individual product names\n", + " for i in range(num_orders):\n", + " while True:\n", + " order = input(f\"Enter the name of product #{i+1} that a customer wants to order: \").strip()\n", + " order_lower = order.lower()\n", + " \n", + " # Check 1: Does the product exist in the store catalog?\n", + " if order_lower not in inventory_lowercase:\n", + " print(f\"Error: '{order}' does not exist in our inventory catalog. Please try a different product.\")\n", + " continue\n", + " \n", + " original_name = inventory_lowercase[order_lower]\n", + " \n", + " # Check 2: Does the product have stock available right now?\n", + " if inventory[original_name] <= 0:\n", + " print(f\"Error: '{original_name}' is currently out of stock! Please choose a different product.\")\n", + " continue\n", + " \n", + " # If all checks pass, add to order records and break the retry loop\n", + " customer_orders.add(original_name)\n", + " break\n", + " \n", + " return customer_orders\n", + "\n", + "\n", + "def update_inventory(customer_orders, inventory):\n", + " \"\"\"Updates inventory counts and uses a comprehension to filter out products with 0 stock.\"\"\"\n", + " # Deduct stock for chosen items (validation in previous step guarantees stock exists)\n", + " for order in customer_orders:\n", + " if order in inventory:\n", + " inventory[order] -= 1\n", + "\n", + " # Dictionary Comprehension: Automatically filters out items whose quantity drops to 0\n", + " updated_inventory = {prod: qty for prod, qty in inventory.items() if qty > 0}\n", + " \n", + " inventory.clear()\n", + " inventory.update(updated_inventory)\n", + "\n", + "\n", + "def calculate_order_statistics(customer_orders, products):\n", + " \"\"\"Calculates order statistics using a list filter operation.\"\"\"\n", + " total_products_ordered = len(customer_orders)\n", + " products_lower = [p.lower() for p in products]\n", + " \n", + " valid_orders_count = sum(1 for order in customer_orders if order.lower() in products_lower)\n", + " \n", + " percentage_ordered = (valid_orders_count / len(products)) * 100 if products else 0.0\n", + " return total_products_ordered, percentage_ordered\n", + "\n", + "\n", + "def print_order_statistics(order_statistics):\n", + " \"\"\"Prints the calculated 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:.1f}%\")\n", + "\n", + "\n", + "def print_updated_inventory(inventory):\n", + " \"\"\"Prints the updated inventory contents cleanly.\"\"\"\n", + " print(\"\\nUpdated Inventory:\")\n", + " for product, quantity in inventory.items():\n", + " print(f\"{product}: {quantity}\")\n", + "\n", + "\n", + "def calculate_total_price(customer_orders):\n", + " \"\"\"2 & 5. Prompts for product prices with error handling and prints the total sum.\"\"\"\n", + " print() # Formatted padding spacing\n", + " total_price = 0.0\n", + " \n", + " for product in customer_orders:\n", + " while True:\n", + " try:\n", + " price = float(input(f\"Enter the price of {product}: \").strip())\n", + " if price < 0:\n", + " print(\"Error: Price cannot be negative. Please try again.\")\n", + " continue\n", + " total_price += price\n", + " break\n", + " except ValueError:\n", + " print(\"Error: Invalid input! Please enter a valid decimal number (e.g., 10.50).\")\n", + " \n", + " print(f\"Total Price: ${total_price:.2f}\")\n", + " return total_price\n", + "\n", + "\n", + "# 7. Main program control flow\n", + "if __name__ == \"__main__\":\n", + " # Base product master catalog array\n", + " products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + " \n", + " # 1. Initialize\n", + " inventory = initialize_inventory(products)\n", + " \n", + " # 2 & 3. Fetch orders (passing active inventory dictionary for cross-checking)\n", + " customer_orders = get_customer_orders(inventory)\n", + " \n", + " # 4. Process analytics and modify system inventory counts\n", + " order_statistics = calculate_order_statistics(customer_orders, products)\n", + " update_inventory(customer_orders, inventory)\n", + " \n", + " # 5 & 6. Print final outputs out to console terminal\n", + " print_order_statistics(order_statistics)\n", + " print_updated_inventory(inventory)\n", + " calculate_total_price(customer_orders)\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "fa28824d", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, @@ -90,7 +295,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.9" } }, "nbformat": 4,