diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index f4c6ef6..9b93332 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -72,13 +72,285 @@ "\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": 1, + "id": "91148ba4-19bc-4c1e-88ad-767b52590020", + "metadata": {}, + "outputs": [], + "source": [ + "# Step 1: Initialize inventory with error handling\n", + "def initialize_inventory(products):\n", + " inventory = {}\n", + "\n", + " for product in products:\n", + " while True:\n", + " try:\n", + " quantity = int(input(f\"Enter the quantity of {product}s available: \"))\n", + " if quantity < 0:\n", + " raise ValueError(\"Quantity cannot be negative.\")\n", + " inventory[product] = quantity\n", + " break\n", + " except ValueError as e:\n", + " print(f\"Error: {e}\")\n", + "\n", + " return inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "19258b52-23a0-4207-aae0-fad07754d0ca", + "metadata": {}, + "outputs": [], + "source": [ + "# Step 2: Get customer orders with error handling\n", + "def get_customer_orders(inventory):\n", + " # validate number of orders\n", + " while True:\n", + " try:\n", + " num_orders = int(input(\"Enter the number of customer orders: \"))\n", + " if num_orders <= 0:\n", + " raise ValueError(\"Number of orders must be greater than 0.\")\n", + " break\n", + " except ValueError as e:\n", + " print(f\"Error: {e}\")\n", + "\n", + " customer_orders = set()\n", + "\n", + " # validate product names\n", + " for _ in range(num_orders):\n", + " while True:\n", + " try:\n", + " product = input(\"Enter the name of a product that a customer wants to order: \").lower()\n", + "\n", + " if product not in inventory:\n", + " raise ValueError(\"Product not found in inventory.\")\n", + "\n", + " if inventory[product] <= 0:\n", + " raise ValueError(\"Product is out of stock.\")\n", + "\n", + " customer_orders.add(product)\n", + " break\n", + "\n", + " except ValueError as e:\n", + " print(f\"Error: {e}\")\n", + "\n", + " return customer_orders" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "0d65dc31-338a-4964-9f3b-cea9acf880f1", + "metadata": {}, + "outputs": [], + "source": [ + "\n", + "# Step 3: Calculate order statistics\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" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "c05bf8f2-32bc-48e8-9928-7ac25d4e4e90", + "metadata": {}, + "outputs": [], + "source": [ + "# Step 4: Update inventory (remove zero stock using comprehension)\n", + "def update_inventory(customer_orders, inventory):\n", + " updated_inventory = {\n", + " product: quantity - 1 if product in customer_orders else quantity\n", + " for product, quantity in inventory.items()\n", + " }\n", + "\n", + " # remove items with 0 quantity\n", + " updated_inventory = {\n", + " product: quantity\n", + " for product, quantity in updated_inventory.items()\n", + " if quantity > 0\n", + " }\n", + "\n", + " return updated_inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "ddcd46ae-1155-4afe-b2dc-377594e6cd34", + "metadata": {}, + "outputs": [], + "source": [ + "# Step 5: Calculate total price with error handling\n", + "def calculate_total_price(customer_orders):\n", + " prices = {}\n", + "\n", + " for product in customer_orders:\n", + " while True:\n", + " try:\n", + " price = float(input(f\"Enter the price of {product}: \"))\n", + " if price < 0:\n", + " raise ValueError(\"Price cannot be negative.\")\n", + " prices[product] = price\n", + " break\n", + " except ValueError as e:\n", + " print(f\"Error: {e}\")\n", + "\n", + " total_price = sum(prices.values())\n", + " return total_price" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "f3cd7876-69c2-4d40-9c58-0502fd14b979", + "metadata": {}, + "outputs": [], + "source": [ + "# Step 6: Print order statistics\n", + "def print_order_statistics(order_statistics):\n", + " print(\"\\nOrder Statistics:\")\n", + " print(f\"Total Products Ordered: {order_statistics[0]}\")\n", + " print(f\"Percentage of Unique Products Ordered: {order_statistics[1]}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "9f03d645-b84b-4a24-931b-54b2a6137b2c", + "metadata": {}, + "outputs": [], + "source": [ + "# Step 7: Print updated inventory\n", + "def print_updated_inventory(inventory):\n", + " print(\"\\nUpdated Inventory:\")\n", + " for product, quantity in inventory.items():\n", + " print(f\"{product}: {quantity}\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "248effad-fdad-4b19-99e2-e92159d32cb3", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity of t-shirts available: 25\n", + "Enter the quantity of mugs available: 5\n", + "Enter the quantity of hats available: 12\n", + "Enter the quantity of books available: 10\n", + "Enter the quantity of keychains available: 5\n", + "Enter the number of customer orders: 20\n", + "Enter the name of a product that a customer wants to order: t-shirts\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: Product not found in inventory.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the name of a product that a customer wants to order: \"t-shirts\"\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: Product not found in inventory.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the name of a product that a customer wants to order: [\"t-shirts\"]\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: Product not found in inventory.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the name of a product that a customer wants to order: mug\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: book\n", + "Enter the name of a product that a customer wants to order: keychain\n", + "Enter the name of a product that a customer wants to order: t-shirts\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: Product not found in inventory.\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the name of a product that a customer wants to order: socks\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: Product not found in inventory.\n" + ] + } + ], + "source": [ + "# MAIN PROGRAM\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", + "inventory = initialize_inventory(products)\n", + "customer_orders = get_customer_orders(inventory)\n", + "\n", + "order_statistics = calculate_order_statistics(customer_orders, products)\n", + "inventory = update_inventory(customer_orders, inventory)\n", + "\n", + "total_price = calculate_total_price(customer_orders)\n", + "\n", + "print_order_statistics(order_statistics)\n", + "print_updated_inventory(inventory)\n", + "print(f\"\\nTotal Price: {total_price}\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "9a2ca367-7613-4fc7-9f67-19055be99a74", + "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 +362,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.9" } }, "nbformat": 4,