diff --git a/lab-python-list-comprehension.ipynb b/lab-python-list-comprehension.ipynb index 5a3c3e1..221fdad 100644 --- a/lab-python-list-comprehension.ipynb +++ b/lab-python-list-comprehension.ipynb @@ -75,11 +75,171 @@ "\n", "```\n" ] + }, + { + "cell_type": "code", + "execution_count": 54, + "id": "069e8713", + "metadata": {}, + "outputs": [], + "source": [ + "#1\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" + ] + }, + { + "cell_type": "code", + "execution_count": 55, + "id": "3b72db1d", + "metadata": {}, + "outputs": [], + "source": [ + "\n", + "#2. Modify the function get_customer_orders so it prompts the user to enter the number of customer orders and gathers the product \n", + "#names using a loop and user input. Use comprehension.\n", + "\n", + "def get_customer_orders(inventory):\n", + " num_orders = int(input(\"Enter the number of customer orders: \"))\n", + " orders = [input(\"Enter the name of a product that a customer wants to order: \").strip().lower() for _ in range(num_orders) ]\n", + " customer_orders = {order for order in orders if order in inventory}\n", + " return customer_orders" + ] + }, + { + "cell_type": "code", + "execution_count": 56, + "id": "88cfbe42", + "metadata": {}, + "outputs": [], + "source": [ + "# 3. Add a new function to calculate the total price of the customer order. For each product in customer_orders, \n", + "# prompt the user to enter the price of that product. Use comprehension to calculate the total price.\n", + "# Note: assume that the user can only have 1 unit of each product.\n", + "\n", + "def calculate_total_price(customer_orders):\n", + " prices = [float(input(f\"Enter the price of {product}: \")) for product in customer_orders]\n", + " total_price = sum(prices)\n", + " return prices,total_price" + ] + }, + { + "cell_type": "code", + "execution_count": 57, + "id": "7beba7f9", + "metadata": {}, + "outputs": [], + "source": [ + "\n", + "#4. Modify the update_inventory function to remove the product from the inventory if its quantity becomes zero after \n", + "# fulfilling the customer orders. Use comprehension to filter out the products with a quantity of zero from the inventory.\n", + "\n", + "def update_inventory(inventory, customer_orders):\n", + " # Reduce the quantity of each ordered product by 1 if it exists\n", + " for order in customer_orders:\n", + " if order in inventory:\n", + " inventory[order] -= 1\n", + "\n", + " # Filter out products with quantity > 0 using comprehension\n", + " inventory = {product: qty for product, qty in inventory.items() if qty > 0}\n", + " return inventory" + ] + }, + { + "cell_type": "markdown", + "id": "46833ba0", + "metadata": {}, + "source": [ + "5. Print the total price of the customer order.\n", + "\n", + "Your code should produce output similar to the following:\n", + "\n", + "```python\n", + "Enter the quantity of t-shirts available: 5\n", + "Enter the quantity of mugs available: 4\n", + "Enter the quantity of hats available: 3\n", + "Enter the quantity of books available: 2\n", + "Enter the quantity of keychains available: 1\n", + "Enter the number of customer orders: 2\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: keychain\n", + "\n", + "Order Statistics:\n", + "Total Products Ordered: 2\n", + "Percentage of Unique Products Ordered: 40.0\n", + "\n", + "Updated Inventory:\n", + "t-shirt: 5\n", + "mug: 4\n", + "hat: 2\n", + "book: 2\n", + "Enter the price of keychain: 5\n", + "Enter the price of hat: 10\n", + "Total Price: 15.0\n", + "\n", + "```" + ] + }, + { + "cell_type": "code", + "execution_count": 58, + "id": "62ffabcc", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Order Statistics:\n", + "Total Products Ordered: 2\n", + "Percentage of Unique Products Ordered: 40.0%\n", + "\n", + "Updated Inventory:\n", + "t-shirt: 5\n", + "mug: 4\n", + "hat: 2\n", + "book: 1\n", + "keychain: 1\n", + "\n", + "Prices entered: [10.0, 15.0]\n", + "Total Price: 25.0\n" + ] + } + ], + "source": [ + "\n", + "def main():\n", + " products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + " inventory = initialize_inventory(products)\n", + " customer_orders = get_customer_orders(inventory)\n", + " \n", + " print(\"\\nOrder Statistics:\")\n", + " print(f\"Total Products Ordered: {len(customer_orders)}\")\n", + " percentage = (len(customer_orders) / len(inventory)) * 100\n", + " print(f\"Percentage of Unique Products Ordered: {percentage:.1f}%\")\n", + "\n", + " inventory = update_inventory(inventory, customer_orders)\n", + "\n", + " print(\"\\nUpdated Inventory:\")\n", + " for product, qty in inventory.items():\n", + " print(f\"{product}: {qty}\")\n", + "\n", + " prices, total_price = calculate_total_price(customer_orders)\n", + " print(f\"\\nPrices entered: {prices}\")\n", + " print(f\"Total Price: {total_price:.1f}\")\n", + "\n", + "\n", + "# Run the program\n", + "if __name__ == \"__main__\":\n", + " main()" + ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -93,7 +253,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.9.23" } }, "nbformat": 4,