diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index f4c6ef6..40060dc 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -72,6 +72,87 @@ "\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": [ + "# Define the products list\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", + "# 1. Initialize Inventory\n", + "def initialize_inventory(products):\n", + " inventory = {product: int(input(f\"Enter the quantity of {product}s available: \")) for product in products}\n", + " return inventory\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", + "# 3. Calculate Total Price\n", + "def calculate_total_price(customer_orders):\n", + " total_price = sum(float(input(f\"Enter the price of {product}: \")) for product in customer_orders)\n", + " return total_price\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", + "# 5. 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)\n", + "\n", + "# 6. Print Order Statistics\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", + "# 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", + "# 8. Main Program\n", + "\n", + "# Step 1: Initialize the inventory\n", + "inventory = initialize_inventory(products)\n", + "\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", + "\n", + "# Step 4: Print order statistics\n", + "print_order_statistics(order_statistics)\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", + "\n", + "# Step 7: Calculate and print total price\n", + "total_price = calculate_total_price(customer_orders)\n", + "print(f\"Total Price: {total_price}\")" + ] } ], "metadata": {