From 0dec383337114413acae5a78b7fbd4c339734deb Mon Sep 17 00:00:00 2001 From: Ghonaim32 Date: Fri, 10 Oct 2025 10:24:09 +0200 Subject: [PATCH] Solved lab --- lab-python-list-comprehension.ipynb | 119 +++++++++++++++++++++++++++- 1 file changed, 117 insertions(+), 2 deletions(-) diff --git a/lab-python-list-comprehension.ipynb b/lab-python-list-comprehension.ipynb index 5a3c3e1..3677f8f 100644 --- a/lab-python-list-comprehension.ipynb +++ b/lab-python-list-comprehension.ipynb @@ -75,11 +75,126 @@ "\n", "```\n" ] + }, + { + "cell_type": "code", + "execution_count": 16, + "id": "057d8f72", + "metadata": {}, + "outputs": [], + "source": [ + "inventory = {}\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", + "# Asking user to enter the quantity per item\n", + "def initialize_inventory(products):\n", + " inventory = {product: int(input(f\"Enter quantity of {product}s available: \")) for product in products}\n", + " print(\"Initialized Inventory:\", inventory)\n", + " return inventory\n", + "\n", + "# Asking user to create a customer order\n", + "def get_customer_orders(products):\n", + " customer_orders = set()\n", + " number_of_items = int(input(\"Enter the number of items you want to order: \"))\n", + " for i in range(number_of_items):\n", + " product_name = input(\"Enter the name of products you want to order: \")\n", + " if product_name in products:\n", + " print(\"Item added to order!\")\n", + " customer_orders.add(product_name)\n", + " else:\n", + " print(\"Please enter a valid item.\")\n", + " print(\"Collected Customer Orders:\", customer_orders)\n", + " return customer_orders\n", + "\n", + "# Updating inventory and removing items no longer available \n", + "def update_inventory(customer_orders, inventory):\n", + " inventory = {product: quantity - 1 if product in customer_orders else quantity for product, quantity in inventory.items()}\n", + " inventory = {product: quantity for product, quantity in inventory.items() if quantity > 0}\n", + " print(\"Updated Inventory:\", inventory)\n", + " return inventory\n" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "id": "a671a3c8", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Initialized Inventory: {'t-shirt': 5, 'mug': 4, 'hat': 3, 'book': 2, 'keychain': 1}\n" + ] + } + ], + "source": [ + "inventory = initialize_inventory(products)" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "id": "b071c8da", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Item added to order!\n", + "Item added to order!\n", + "Collected Customer Orders: {'keychain', 'hat'}\n" + ] + } + ], + "source": [ + "customer_orders = get_customer_orders(products)" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "id": "9d61bb62", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Updated Inventory: {'t-shirt': 5, 'mug': 4, 'hat': 2, 'book': 2}\n" + ] + } + ], + "source": [ + "updated_inventory = update_inventory(customer_orders, inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "id": "0c88afea", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The total price of this order is: 15\n" + ] + } + ], + "source": [ + "price_hat = int(input(\"Enter price of hat: \"))\n", + "price_keychain = int(input(\"Enter price of keychain: \"))\n", + "total_price = price_hat + price_keychain\n", + "print(f\"The total price of this order is: {total_price}\")" + ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -93,7 +208,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.7" } }, "nbformat": 4,