From 67bd40339fd9550ee4b334297daf76a77d4689bd Mon Sep 17 00:00:00 2001 From: veerpal Date: Thu, 27 Nov 2025 14:48:54 +0100 Subject: [PATCH] lab complete --- lab-python-flow-control.ipynb | 156 +++++++++++++++++++++++++++++++++- 1 file changed, 155 insertions(+), 1 deletion(-) diff --git a/lab-python-flow-control.ipynb b/lab-python-flow-control.ipynb index f4c7391..0f33b9b 100644 --- a/lab-python-flow-control.ipynb +++ b/lab-python-flow-control.ipynb @@ -37,6 +37,160 @@ "\n", "3. Instead of updating the inventory by subtracting 1 from the quantity of each product, only do it for the products that were ordered (those in \"customer_orders\")." ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "c19eb94b-45ee-412a-bba4-23661e7d3237", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['t-shirt', 'mug', 'hat', 'book', 'keychain']\n" + ] + }, + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter number 1: 12\n", + "Enter number 2: 23\n", + "Enter number 3: 34\n", + "Enter number 4: 11\n", + "Enter number 5: 36\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['12', '23', '34', '11', '36']\n", + "{'t-shirt': 12, 'mug': 23, 'hat': 34, 'book': 11, 'keychain': 36}\n" + ] + } + ], + "source": [ + "# 1. List Products\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "print(products)\n", + "inventory = {}\n", + "values = []\n", + "\n", + "for i in range(0,5):\n", + " user_num = input(f\"Enter number {i+1}: \")\n", + " if user_num.isdigit():\n", + " values.append(user_num)\n", + " else:\n", + " print(\"Enter the valid value\")\n", + " user_num = input(f\"Enter number {i+1}: \")\n", + " values.append(user_num)\n", + "print(values)\n", + "\n", + "for i in range(0,5):\n", + " inventory[products[i]] = int(values[i])\n", + "print(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "1cc9b843-66c1-43f8-961a-4e0505f11095", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the product 1: book\n", + "Do you want to add another product? (yes/no): yes\n", + "Enter the product 2: hat\n", + "Do you want to add another product? (yes/no): no\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Customer orders: {'hat', 'book'}\n" + ] + } + ], + "source": [ + "customer_orders = set()\n", + "\n", + "for i in range(0,3):\n", + " # a. Prompt the user to enter the name of a product.\n", + " product = input(f\"Enter the product {i+1}: \")\n", + " # b. Add the product to the \"customer_orders\" set.\n", + " if product.isdigit():\n", + " product = print(f\"Enter the valid product {i+1}:\")\n", + " product = input(f\"Enter the product {i+1} that you want to order : \")\n", + " customer_orders.add(product)\n", + " else:\n", + " customer_orders.add(product)\n", + " another_product = input(\"Do you want to add another product? (yes/no): \").strip().lower()\n", + "\n", + " # c. Continue only if yes\n", + " if another_product != \"yes\":\n", + " break\n", + "\n", + "\n", + "print(\"Customer orders:\", customer_orders)" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "d4d94076-8328-4784-a0d8-ad15aa8c0436", + "metadata": {}, + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter the quantity for the product hat: 3\n", + "Enter the quantity for the product book: 2\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 12, 'mug': 23, 'hat': 31, 'book': 7, 'keychain': 36}\n" + ] + } + ], + "source": [ + "products = []\n", + "\n", + "for product in customer_orders:\n", + " while True:\n", + " quantity = input(f\"Enter the quantity for the product {product}: \")\n", + " if quantity.isdigit():\n", + " quantity = int(quantity)\n", + " break\n", + " print(\"Please enter a valid numeric quantity.\")\n", + " \n", + " products.append(quantity)\n", + "\n", + " # Update inventory if product exists\n", + " if product in inventory:\n", + " inventory[product] = int(inventory[product]) - quantity\n", + " else:\n", + " print(f\"Warning: {product} not found in inventory!\")\n", + "\n", + "print(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "87a1c85a-d62a-400c-83ac-3b7cb106003b", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { @@ -55,7 +209,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.12.4" } }, "nbformat": 4,