From cc1948a9ad57ae22e08c62ef1b863ccaac7c6e97 Mon Sep 17 00:00:00 2001 From: Anna Igumnova Date: Mon, 20 Oct 2025 20:16:06 +0200 Subject: [PATCH 1/2] Lab completed --- lab-python-flow-control.ipynb | 180 +++++++++++++++++++++++++++++++++- 1 file changed, 178 insertions(+), 2 deletions(-) diff --git a/lab-python-flow-control.ipynb b/lab-python-flow-control.ipynb index f4c7391..0782655 100644 --- a/lab-python-flow-control.ipynb +++ b/lab-python-flow-control.ipynb @@ -37,11 +37,187 @@ "\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": 1, + "id": "be666ab6", + "metadata": {}, + "outputs": [], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "inventory = {} # empty dictionary" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "2bacc7d8", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 4, 'mug': 6, 'hat': 2, 'book': 8, 'keychain': 5}\n" + ] + } + ], + "source": [ + "inventory[products[0]] = int(input(\"Enter the amount of t-shirts: \"))\n", + "inventory[products[1]] = int(input(\"Enter the amount of mugs: \"))\n", + "inventory[products[2]] = int(input(\"Enter the amount of hats: \"))\n", + "inventory[products[3]] = int(input(\"Enter the amount of books: \"))\n", + "inventory[products[4]] = int(input(\"Enter the amount of keychains: \"))\n", + "print(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "id": "5d5088d9", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Your order: {'book', 'mug', 'keychain'}\n" + ] + } + ], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "customer_orders = set() # empty set\n", + "add_more = 'yes' # adding a condition for the loop\n", + "while add_more == 'yes':\n", + " product = input(\"Enter the product you want to order: \")\n", + " if product in products:\n", + " customer_orders.add(product)\n", + " else:\n", + " print(\"product not found, try again\") # how to repeat this untill a product from the list is chosen?\n", + " add_more = input(\"Do you want to order more: yes/no ?\")\n", + "print(\"Your order: \", customer_orders)" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "id": "5f7aeafb", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3\n", + "5\n", + "60.0\n" + ] + } + ], + "source": [ + "products_ordered = len(customer_orders)\n", + "print(products_ordered)\n", + "\n", + "avaliable_products = len(products)\n", + "print(avaliable_products)\n", + "\n", + "percentage = (products_ordered/avaliable_products)*100\n", + "print(percentage)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "id": "034b1461", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Order status: ('ordered products:', 3, 'products avaliable:', 5)\n" + ] + } + ], + "source": [ + "order_status = (\"ordered products:\", products_ordered, \"products avaliable:\", avaliable_products)\n", + "print(\"Order status: \", order_status)" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "id": "b66a9227", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 4, 'mug': 6, 'hat': 2, 'book': 8, 'keychain': 5}\n", + "{'book', 'mug', 'keychain'}\n", + "dict_keys(['t-shirt', 'mug', 'hat', 'book', 'keychain'])\n" + ] + } + ], + "source": [ + "print(inventory)\n", + "print(customer_orders)\n", + "print(inventory.keys())" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "id": "413deea2", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'t-shirt': 4, 'mug': 5, 'hat': 2, 'book': 7, 'keychain': 4}\n" + ] + } + ], + "source": [ + "for product in customer_orders:\n", + " if product in inventory and inventory[product] > 0:\n", + " inventory[product] -= 1\n", + "print(inventory)" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "id": "ecb4141c", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Updated inventory:\n", + "t-shirt: 4\n", + "mug: 5\n", + "hat: 2\n", + "book: 7\n", + "keychain: 4\n" + ] + } + ], + "source": [ + "print(\"Updated inventory:\")\n", + "for position, amount in inventory.items():\n", + " print(position + \": \" + str(amount))" + ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, @@ -55,7 +231,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.5" } }, "nbformat": 4, From 6f3419c18cee97133119afb6e5843544c72026a5 Mon Sep 17 00:00:00 2001 From: Anna Igumnova Date: Mon, 20 Oct 2025 20:47:50 +0200 Subject: [PATCH 2/2] Added a code in question To repeat the code untill the product in the list is chosen --- lab-python-flow-control.ipynb | 42 +++++++++++++++++------------------ 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/lab-python-flow-control.ipynb b/lab-python-flow-control.ipynb index 0782655..59d061a 100644 --- a/lab-python-flow-control.ipynb +++ b/lab-python-flow-control.ipynb @@ -40,7 +40,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 11, "id": "be666ab6", "metadata": {}, "outputs": [], @@ -51,7 +51,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 12, "id": "2bacc7d8", "metadata": {}, "outputs": [ @@ -59,7 +59,7 @@ "name": "stdout", "output_type": "stream", "text": [ - "{'t-shirt': 4, 'mug': 6, 'hat': 2, 'book': 8, 'keychain': 5}\n" + "{'t-shirt': 6, 'mug': 4, 'hat': 8, 'book': 5, 'keychain': 7}\n" ] } ], @@ -74,7 +74,7 @@ }, { "cell_type": "code", - "execution_count": 21, + "execution_count": null, "id": "5d5088d9", "metadata": {}, "outputs": [ @@ -82,7 +82,7 @@ "name": "stdout", "output_type": "stream", "text": [ - "Your order: {'book', 'mug', 'keychain'}\n" + "Your order: {'hat', 'book', 't-shirt'}\n" ] } ], @@ -92,17 +92,17 @@ "add_more = 'yes' # adding a condition for the loop\n", "while add_more == 'yes':\n", " product = input(\"Enter the product you want to order: \")\n", - " if product in products:\n", - " customer_orders.add(product)\n", + " if product not in products:\n", + " print(\"product not found, try again\") # added repetition till the product is chosen from the list\n", " else:\n", - " print(\"product not found, try again\") # how to repeat this untill a product from the list is chosen?\n", + " customer_orders.add(product) \n", " add_more = input(\"Do you want to order more: yes/no ?\")\n", "print(\"Your order: \", customer_orders)" ] }, { "cell_type": "code", - "execution_count": 22, + "execution_count": 16, "id": "5f7aeafb", "metadata": {}, "outputs": [ @@ -129,7 +129,7 @@ }, { "cell_type": "code", - "execution_count": 23, + "execution_count": 17, "id": "034b1461", "metadata": {}, "outputs": [ @@ -148,7 +148,7 @@ }, { "cell_type": "code", - "execution_count": 24, + "execution_count": 18, "id": "b66a9227", "metadata": {}, "outputs": [ @@ -156,8 +156,8 @@ "name": "stdout", "output_type": "stream", "text": [ - "{'t-shirt': 4, 'mug': 6, 'hat': 2, 'book': 8, 'keychain': 5}\n", - "{'book', 'mug', 'keychain'}\n", + "{'t-shirt': 6, 'mug': 4, 'hat': 8, 'book': 5, 'keychain': 7}\n", + "{'hat', 'book', 't-shirt'}\n", "dict_keys(['t-shirt', 'mug', 'hat', 'book', 'keychain'])\n" ] } @@ -170,7 +170,7 @@ }, { "cell_type": "code", - "execution_count": 25, + "execution_count": 19, "id": "413deea2", "metadata": {}, "outputs": [ @@ -178,7 +178,7 @@ "name": "stdout", "output_type": "stream", "text": [ - "{'t-shirt': 4, 'mug': 5, 'hat': 2, 'book': 7, 'keychain': 4}\n" + "{'t-shirt': 5, 'mug': 4, 'hat': 7, 'book': 4, 'keychain': 7}\n" ] } ], @@ -191,7 +191,7 @@ }, { "cell_type": "code", - "execution_count": 26, + "execution_count": 20, "id": "ecb4141c", "metadata": {}, "outputs": [ @@ -200,11 +200,11 @@ "output_type": "stream", "text": [ "Updated inventory:\n", - "t-shirt: 4\n", - "mug: 5\n", - "hat: 2\n", - "book: 7\n", - "keychain: 4\n" + "t-shirt: 5\n", + "mug: 4\n", + "hat: 7\n", + "book: 4\n", + "keychain: 7\n" ] } ],