diff --git a/lab-python-list-comprehension.ipynb b/lab-python-list-comprehension.ipynb index 5a3c3e1..6d57737 100644 --- a/lab-python-list-comprehension.ipynb +++ b/lab-python-list-comprehension.ipynb @@ -75,11 +75,193 @@ "\n", "```\n" ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d754aa77", + "metadata": {}, + "outputs": [], + "source": [ + "def initialize_inventory(products):\n", + " inventory = {}\n", + " for product in products:\n", + " quantity = int(input(f\"Enter the quantity of {product} available: \"))\n", + " inventory[product] = quantity\n", + " return inventory\n" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "4b5d0faf", + "metadata": {}, + "outputs": [], + "source": [ + "def initialize_inventory(products):\n", + " # products is expected to be a list of strings (e.g., ['t-shirts', 'hats', 'keychain'])\n", + " inventory = {\n", + " product: int(input(f\"Enter the quantity of {product} available: \")) \n", + " for product in products\n", + " }\n", + " return inventory\n" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "356d8849", + "metadata": {}, + "outputs": [], + "source": [ + "def get_customer_orders():\n", + " try:\n", + " # Prompt for the number of orders\n", + " num_orders = int(input(\"Enter the total number of customer orders: \"))\n", + " except ValueError:\n", + " print(\"Invalid input. Assuming 0 orders.\")\n", + " return []\n", + "\n", + " # Use a List Comprehension to gather the product name for each order\n", + " customer_orders = [\n", + " input(f\"Enter the name of product for order #{i+1}: \") \n", + " for i in range(num_orders)\n", + " ]\n", + " return customer_orders\n", + "\n", + "# Example of how it should work (based on the image output):\n", + "# Enter the total number of customer orders: 4\n", + "# Enter the name of product for order #1: t-shirt\n", + "# Enter the name of product for order #2: hat\n", + "# Enter the name of product for order #3: t-shirt\n", + "# Enter the name of product for order #4: keychain\n" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "184ef433", + "metadata": {}, + "outputs": [], + "source": [ + "def calculate_total_price(customer_orders):\n", + " # Use a Generator Expression within the sum() function\n", + " total_price = sum(\n", + " float(input(f\"Enter the price of {product}: \"))\n", + " for product in customer_orders\n", + " )\n", + " return total_price\n" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "24765ef2", + "metadata": {}, + "outputs": [], + "source": [ + "def update_inventory(inventory, customer_orders):\n", + " # 1. Fulfill the orders (standard loop is best for this update)\n", + " for product in customer_orders:\n", + " if product in inventory and inventory[product] > 0:\n", + " inventory[product] -= 1\n", + " else:\n", + " print(f\"Warning: Cannot fulfill order for {product}. Item not in stock or not found.\")\n", + "\n", + " # 2. Use Dictionary Comprehension to filter out products with zero quantity\n", + " # This creates a *new* dictionary that excludes items where the value is 0.\n", + " updated_inventory = {\n", + " product: quantity\n", + " for product, quantity in inventory.items()\n", + " if quantity > 0\n", + " }\n", + " \n", + " return updated_inventory\n" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "ce22e825", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "--- Inventory Initialization ---\n", + "\n", + "--- Customer Orders ---\n", + "\n", + "--- Price Calculation ---\n", + "Warning: Cannot fulfill order for hat. Item not in stock or not found.\n", + "\n", + "--- Order Statistics ---\n", + "Percentage of Unique Products Ordered: 100.0%\n", + "\n", + "--- Updated Inventory ---\n", + "t-shirts: 5\n", + "mug: 4\n", + "hats: 2\n", + "book: 2\n", + "\n", + "Total Price: 15.00\n" + ] + } + ], + "source": [ + "# --- Setup Data (Based on image input) ---\n", + "PRODUCTS = ['t-shirts', 'mug', 'hats', 'book', 'keychain']\n", + "\n", + "# 1. Initialize Inventory\n", + "print(\"--- Inventory Initialization ---\")\n", + "# User input for quantities: t-shirts: 5, hats: 3, keychain: 2\n", + "initial_inventory = initialize_inventory(PRODUCTS)\n", + "\n", + "# 2. Get Customer Orders\n", + "print(\"\\n--- Customer Orders ---\")\n", + "# User input for 4 orders: t-shirt, hat, t-shirt, keychain\n", + "customer_orders = get_customer_orders()\n", + "\n", + "# 3. Calculate Total Price\n", + "print(\"\\n--- Price Calculation ---\")\n", + "# User input for prices: t-shirt: 10.0, hat: 15.0, t-shirt: 10.0, keychain: 5.0\n", + "total_price = calculate_total_price(customer_orders)\n", + "\n", + "# 4. Update Inventory\n", + "updated_inventory = update_inventory(initial_inventory, customer_orders)\n", + "\n", + "# --- Output the Results ---\n", + "print(\"\\n--- Order Statistics ---\")\n", + "# Use a Set Comprehension to find unique products\n", + "unique_products_ordered = {product for product in customer_orders}\n", + "percentage_unique = (len(unique_products_ordered) / len(customer_orders)) * 100 if customer_orders else 0\n", + "\n", + "print(f\"Percentage of Unique Products Ordered: {percentage_unique:.1f}%\")\n", + "\n", + "print(\"\\n--- Updated Inventory ---\")\n", + "for product, quantity in updated_inventory.items():\n", + " print(f\"{product}: {quantity}\")\n", + " \n", + "print(f\"\\nTotal Price: {total_price:.2f}\")\n", + "\n", + "# Expected Output based on the image's example inputs:\n", + "# Initial Quantities: t-shirts: 5, hats: 3, keychain: 2\n", + "# Orders: t-shirt, hat, t-shirt, keychain (4 total orders)\n", + "# Inventory After Orders:\n", + "# t-shirts: 5 - 2 = 3\n", + "# hats: 3 - 1 = 2\n", + "# keychain: 2 - 1 = 1\n", + "# Prices: 10 + 15 + 10 + 5 = 40.0\n", + "# Unique Orders: t-shirt, hat, keychain (3 unique)\n", + "# Percentage: (3 / 4) * 100 = 75.0%\n", + "# (NOTE: The image output shows different numbers, perhaps a slight difference in initial input or the example is older.)\n" + ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, @@ -93,7 +275,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.5" } }, "nbformat": 4,