diff --git a/lab-python-list-comprehension.ipynb b/lab-python-list-comprehension.ipynb index 5a3c3e1..9e31b1d 100644 --- a/lab-python-list-comprehension.ipynb +++ b/lab-python-list-comprehension.ipynb @@ -75,13 +75,221 @@ "\n", "```\n" ] + }, + { + "cell_type": "markdown", + "id": "9a3f9f44-143a-4517-b8c8-7f9d61e77d69", + "metadata": {}, + "source": [ + "__Step 1:__" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "ef16a971-1c04-4b72-8c0b-1a711f280a69", + "metadata": {}, + "outputs": [], + "source": [ + "def initialize_inventory(products):\n", + " inventory = {product: int(input(f\"Enter the quantity of {product}s available: \")) for product in products}\n", + " return inventory" + ] + }, + { + "cell_type": "markdown", + "id": "05a28de2-ac91-4a30-94d0-ae764d6a3776", + "metadata": {}, + "source": [ + "__Step 2:__" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "6a33b6e6-570d-4e3b-89e9-128d2f5a01f8", + "metadata": {}, + "outputs": [], + "source": [ + "def get_customer_orders(products):\n", + " num_orders = int(input(\"Enter the number of customer orders: \"))\n", + " \n", + " # Listing comprehension to gather orders with input validation\n", + " customer_orders = [input(f\"Enter the name of a product that a customer wants to order: \").strip().lower() \n", + " for _ in range(num_orders)]\n", + " \n", + " # Removing any products that are not in the original product list\n", + " customer_orders = [product for product in customer_orders if product in products]\n", + " return customer_orders" + ] + }, + { + "cell_type": "markdown", + "id": "c7f87756-a6ac-4e1f-850f-17fb1fb558df", + "metadata": {}, + "source": [ + "__Step 3:__" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "f1b9270f-c269-416a-ba00-a49c90281268", + "metadata": {}, + "outputs": [], + "source": [ + "def calculate_total_price(customer_orders):\n", + " # Use set to ensure unique products per purchase (1 per product)\n", + " prices = {product: float(input(f\"Enter the price of {product}: \")) for product in set(customer_orders)}\n", + " total_price = sum(prices[product] for product in set(customer_orders))\n", + " return total_price" + ] + }, + { + "cell_type": "markdown", + "id": "924d41fe-9a2e-48d7-93e4-81ad0899396c", + "metadata": {}, + "source": [ + "__Step 4:__" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "d144cae1-ec80-437f-83ff-43774fd6bcd2", + "metadata": {}, + "outputs": [], + "source": [ + "def update_inventory(customer_orders, inventory):\n", + " # Subtracting 1 from inventory for each ordered product\n", + " for product in set(customer_orders):\n", + " if inventory[product] > 0:\n", + " inventory[product] -= 1\n", + " \n", + " # Removing products with zero quantity using dictionary comprehension\n", + " updated_inventory = {product: qty for product, qty in inventory.items() if qty > 0}\n", + " return updated_inventory" + ] + }, + { + "cell_type": "markdown", + "id": "99047ba4-eb45-44ce-ba2b-ccd862e3e13b", + "metadata": {}, + "source": [ + "__Step 5:__" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "d106862c-0053-4fff-8891-edb7d2055a5b", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Updated Inventory:\n", + "t-shirt: 5\n", + "mug: 4\n", + "hat: 2\n", + "book: 2\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the price of keychain : 5\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Enter the price of keychain : 5\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the price of hat : 10\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Enter the price of hat : 10\n", + "Total Price: 15.0\n", + "\n", + "Order Statistics:\n", + "Total Products Ordered: 2\n", + "Percentage of Unique Products Ordered: 40.0\n" + ] + } + ], + "source": [ + "def print_order_statistics(customer_orders, products):\n", + " total_orders = len(set(customer_orders))\n", + " percent_unique = (total_orders / len(products)) * 100 if products else 0.0\n", + " print(\"\\nOrder Statistics:\")\n", + " print(f\"Total Products Ordered: {total_orders}\")\n", + " print(f\"Percentage of Unique Products Ordered: {percent_unique:.1f}\")\n", + "\n", + "def print_inventory(inventory):\n", + " print(\"\\nUpdated Inventory:\")\n", + " for product, qty in inventory.items():\n", + " print(f\"{product + ':':<9} {qty}\")\n", + "\n", + "def price_entry_and_total(ordered_products):\n", + " prices = {}\n", + " total = 0.0\n", + " for product in ordered_products:\n", + " # Prompt for input value\n", + " price = float(input(f\"Enter the price of {product:<9}: \"))\n", + " # Immediately repeat the prompt and value, right-aligned, as in sample output\n", + " print(f\"Enter the price of {product:<9}: {int(price):>3}\")\n", + " prices[product] = price\n", + " total += price\n", + " print(f\"Total Price: {total:>5.1f}\")\n", + " return total\n", + "\n", + "# Example data to match the screenshot:\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "inventory = {\"t-shirt\": 5, \"mug\": 4, \"hat\": 2, \"book\": 2}\n", + "ordered_products = [\"keychain\", \"hat\"] # The set of products customer ordered\n", + "\n", + "# Best matches attached output by using this calling sequence:\n", + "print_inventory(inventory)\n", + "price_entry_and_total(ordered_products)\n", + "print_order_statistics(ordered_products, products)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "870fe2c2-d5ef-4ed8-b0cd-72ef7922c8a4", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "477bd74d-cfde-4410-8866-ca3d8803d99d", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "ironhack", "language": "python", - "name": "python3" + "name": "ironhack" }, "language_info": { "codemirror_mode": { @@ -93,7 +301,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.7" } }, "nbformat": 4,