From 53b542b02632950f770664ef70eddcb33cda548b Mon Sep 17 00:00:00 2001 From: Olga Galanina Date: Sat, 17 Jan 2026 15:37:44 +0100 Subject: [PATCH 1/2] Week1Lab2_Done --- lab-python-flow-control.ipynb | 106 +++++++++++++++++++++++++++++++++- 1 file changed, 104 insertions(+), 2 deletions(-) diff --git a/lab-python-flow-control.ipynb b/lab-python-flow-control.ipynb index f4c7391..99d9f5f 100644 --- a/lab-python-flow-control.ipynb +++ b/lab-python-flow-control.ipynb @@ -37,11 +37,113 @@ "\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": null, + "id": "47d844ac", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'t-shirt': 34, 'mug': 56, 'hat': 78, 'book': 23, 'keychain': 87}" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "\n", + "inventory = {}\n", + "\n", + "#Ask the user to input the quantity of each product available in the inventory. Use the product names from the `products` list as keys in the `inventory` dictionary and assign the respective quantities as values.\n", + "for product in products:\n", + " quantity = int(input(f\"Enter the quantity for {product}: \"))\n", + " inventory[product] = quantity #add to the dictionairy: key = assign the value\n", + "\n", + "inventory\n" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "75e8fca1", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "All products added to the order.\n", + "Customer order: {'hat'}\n" + ] + } + ], + "source": [ + "#a. Prompt the user to enter the name of a product that a customer wants to order.\n", + "#b. Add the product name to the \"customer_orders\" set.\n", + "#c. Ask the user if they want to add another product (yes/no).\n", + "#d. Continue the loop until the user does not want to add another product.\n", + "\n", + "customer_order = set()\n", + "\n", + "while True:\n", + " p = input(\"Enter the product you want to order: \")\n", + " customer_order.add(p)\n", + "\n", + " user_input = input(\"Do you want to add another product? (yes/no): \").lower()\n", + "\n", + " if user_input == \"yes\":\n", + " continue\n", + " else:\n", + " print(\"All products added to the order.\")\n", + " break\n", + "\n", + "print(\"Customer order:\", customer_order)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "b2271121", + "metadata": {}, + "outputs": [ + { + "ename": "NameError", + "evalue": "name 'inventory' is not defined", + "output_type": "error", + "traceback": [ + "\u001b[31m---------------------------------------------------------------------------\u001b[39m", + "\u001b[31mNameError\u001b[39m Traceback (most recent call last)", + "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[11]\u001b[39m\u001b[32m, line 2\u001b[39m\n\u001b[32m 1\u001b[39m \u001b[38;5;66;03m#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\").\u001b[39;00m\n\u001b[32m----> \u001b[39m\u001b[32m2\u001b[39m \u001b[43minventory\u001b[49m\n\u001b[32m 4\u001b[39m \u001b[38;5;28;01mfor\u001b[39;00m product \u001b[38;5;129;01min\u001b[39;00m customer_order:\n\u001b[32m 5\u001b[39m \u001b[38;5;28;01mif\u001b[39;00m product \u001b[38;5;129;01min\u001b[39;00m inventory:\n", + "\u001b[31mNameError\u001b[39m: name 'inventory' is not defined" + ] + } + ], + "source": [ + "#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\").\n", + "inventory\n", + "\n", + "for product in customer_order:\n", + " if product in inventory:\n", + " inventory[product] -= 1\n", + " else:\n", + " print({product}, \"is not in the inventory.\")\n", + "\n", + "print(\"Updated inventory:\")\n", + "for product, quantity in inventory.items():\n", + " print(product, \":\", quantity)\n", + "\n" + ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, @@ -55,7 +157,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.9" } }, "nbformat": 4, From 998571c67e8a1c49b1c6a0f040929969300fe6dc Mon Sep 17 00:00:00 2001 From: Olga Galanina Date: Sat, 17 Jan 2026 17:32:15 +0100 Subject: [PATCH 2/2] Week1Lab2_Done --- lab-python-flow-control.ipynb | 75 +++++++++-------------------------- 1 file changed, 18 insertions(+), 57 deletions(-) diff --git a/lab-python-flow-control.ipynb b/lab-python-flow-control.ipynb index 99d9f5f..93ab5dc 100644 --- a/lab-python-flow-control.ipynb +++ b/lab-python-flow-control.ipynb @@ -40,22 +40,28 @@ }, { "cell_type": "code", - "execution_count": null, - "id": "47d844ac", + "execution_count": 15, + "id": "b2271121", "metadata": {}, "outputs": [ { - "data": { - "text/plain": [ - "{'t-shirt': 34, 'mug': 56, 'hat': 78, 'book': 23, 'keychain': 87}" - ] - }, - "execution_count": 1, - "metadata": {}, - "output_type": "execute_result" + "name": "stdout", + "output_type": "stream", + "text": [ + "List of inventory: {'t-shirt': 45, 'mug': 87, 'hat': 90, 'book': 23, 'keychain': 44}\n", + "All products added to the order.\n", + "Customer order: {'mug', 'book'}\n", + "Updated inventory:\n", + "t-shirt : 45\n", + "mug : 86\n", + "hat : 90\n", + "book : 22\n", + "keychain : 44\n" + ] } ], "source": [ + "#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\").\n", "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", "\n", "inventory = {}\n", @@ -65,29 +71,7 @@ " quantity = int(input(f\"Enter the quantity for {product}: \"))\n", " inventory[product] = quantity #add to the dictionairy: key = assign the value\n", "\n", - "inventory\n" - ] - }, - { - "cell_type": "code", - "execution_count": 10, - "id": "75e8fca1", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "All products added to the order.\n", - "Customer order: {'hat'}\n" - ] - } - ], - "source": [ - "#a. Prompt the user to enter the name of a product that a customer wants to order.\n", - "#b. Add the product name to the \"customer_orders\" set.\n", - "#c. Ask the user if they want to add another product (yes/no).\n", - "#d. Continue the loop until the user does not want to add another product.\n", + "print(\"List of inventory: \", inventory)\n", "\n", "customer_order = set()\n", "\n", @@ -103,30 +87,7 @@ " print(\"All products added to the order.\")\n", " break\n", "\n", - "print(\"Customer order:\", customer_order)\n" - ] - }, - { - "cell_type": "code", - "execution_count": 11, - "id": "b2271121", - "metadata": {}, - "outputs": [ - { - "ename": "NameError", - "evalue": "name 'inventory' is not defined", - "output_type": "error", - "traceback": [ - "\u001b[31m---------------------------------------------------------------------------\u001b[39m", - "\u001b[31mNameError\u001b[39m Traceback (most recent call last)", - "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[11]\u001b[39m\u001b[32m, line 2\u001b[39m\n\u001b[32m 1\u001b[39m \u001b[38;5;66;03m#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\").\u001b[39;00m\n\u001b[32m----> \u001b[39m\u001b[32m2\u001b[39m \u001b[43minventory\u001b[49m\n\u001b[32m 4\u001b[39m \u001b[38;5;28;01mfor\u001b[39;00m product \u001b[38;5;129;01min\u001b[39;00m customer_order:\n\u001b[32m 5\u001b[39m \u001b[38;5;28;01mif\u001b[39;00m product \u001b[38;5;129;01min\u001b[39;00m inventory:\n", - "\u001b[31mNameError\u001b[39m: name 'inventory' is not defined" - ] - } - ], - "source": [ - "#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\").\n", - "inventory\n", + "print(\"Customer order:\", customer_order)\n", "\n", "for product in customer_order:\n", " if product in inventory:\n",