From 82bca950412b1c110bf4e4584a2f6e122d54d762 Mon Sep 17 00:00:00 2001 From: PaulinaMamiaga <228375023+PaulinaMamiaga@users.noreply.github.com> Date: Thu, 6 Nov 2025 10:53:53 +0100 Subject: [PATCH 1/2] Update lab Phyton Flow Control --- lab-python-flow-control.ipynb | 534 +++++++++++++++++++++++++++++++++- 1 file changed, 532 insertions(+), 2 deletions(-) diff --git a/lab-python-flow-control.ipynb b/lab-python-flow-control.ipynb index f4c7391..2eaa0b0 100644 --- a/lab-python-flow-control.ipynb +++ b/lab-python-flow-control.ipynb @@ -37,11 +37,541 @@ "\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": "markdown", + "id": "4488e8c4", + "metadata": {}, + "source": [ + "-----\n", + "# Managing Customer Orders Optimized\n", + "-----\n", + "By Paulina Mamiaga Iyanga / Ironhack Data Analytics Bootcamp Student\n", + "\n", + "--------" + ] + }, + { + "cell_type": "markdown", + "id": "b5e6be4c", + "metadata": {}, + "source": [ + "## LAB SOLUTION:\n", + "------" + ] + }, + { + "cell_type": "markdown", + "id": "ccaf3281", + "metadata": {}, + "source": [ + "### Introduction:" + ] + }, + { + "cell_type": "markdown", + "id": "9f5b6fcd", + "metadata": {}, + "source": [ + "> **Optimized Version Summary**\n", + ">\n", + "> In this **optimized version**, we keep the main structure from the previous lab \n", + "> (*product list*, *inventory creation*, *order statistics*, and *inventory update*). \n", + ">\n", + "> - We optimize the code by using **loops** to remove repetition and improve flexibility. \n", + "> - Instead of asking for **three fixed products**, we now use a **while loop** that lets \n", + "> the user add **as many products as they want** — as long as the products are available \n", + "> in our predefined **`products`** list — improving both logic and user experience.\n" + ] + }, + { + "cell_type": "markdown", + "id": "4b0b583c", + "metadata": {}, + "source": [ + "---\n", + "1. Define a list called `products` that contains the following items: \"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\". Code from lab *Data structures*." + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "id": "34e01cce", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['t-shirt', 'mug', 'hat', 'book', 'keychain']\n" + ] + } + ], + "source": [ + "# define list products\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "print(products)" + ] + }, + { + "cell_type": "markdown", + "id": "b051970b", + "metadata": {}, + "source": [ + "-----\n", + "2. Create an empty dictionary called `inventory`. Code from lab *Data structures*." + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "id": "e4f81bdf", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{}\n" + ] + } + ], + "source": [ + "# create empty dict inventory\n", + "inventory = {}\n", + "print(inventory)\n" + ] + }, + { + "cell_type": "markdown", + "id": "285daecf", + "metadata": {}, + "source": [ + "-----\n", + "3. Ask the user to input the quantity of each product available in the inventory. \n", + "Use the product names from the `products` list as keys in the `inventory` dictionary and assign the respective quantities as values. Code from lab *Data structures*." + ] + }, + { + "cell_type": "code", + "execution_count": 44, + "id": "9499751d", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "- t-shirt quantity updated to 10.\n", + "- mug quantity updated to 10.\n", + "- hat quantity updated to 10.\n", + "- book quantity updated to 10.\n", + "- keychain quantity updated to 10.\n", + "\n", + "Updated Inventory:\n", + "- t-shirt: 10\n", + "- mug: 10\n", + "- hat: 10\n", + "- book: 10\n", + "- keychain: 10\n" + ] + } + ], + "source": [ + "#3. user inputs the quantity of each product available in the inventory:\n", + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "inventory = {}\n", + "for product in products:\n", + " quantity = int(input(f\"Enter the quantity for {product}: \"))\n", + " inventory[product] = quantity\n", + " print(f\"- {product} quantity updated to {quantity}.\")\n", + "\n", + "# print each product and quantity vertically.\n", + "# Here we only optimize the code by printing vertically in one go, rather than in horizontal way as before. \n", + "print(\"\\nUpdated Inventory:\")\n", + "for product, quantity in inventory.items():\n", + " print(f\"- {product}: {quantity}\")\n" + ] + }, + { + "cell_type": "markdown", + "id": "f4422c08", + "metadata": {}, + "source": [ + "-----\n", + "4. Create an empty set called `customer_orders`. Code from lab *Data structures*." + ] + }, + { + "cell_type": "code", + "execution_count": 45, + "id": "6414b0a1", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "set()\n" + ] + } + ], + "source": [ + "# initize empty set for customer orders\n", + "customer_orders = set()\n", + "print(customer_orders)" + ] + }, + { + "cell_type": "markdown", + "id": "e3ee6beb", + "metadata": {}, + "source": [ + "-----\n", + "5. **HERE is where we optimize our code.**\n", + "\n", + "- In the previous lab (*Data Structures*), we asked the user to input the names of three products that a customer wanted to order (from the list of available products: \"t-shirt\", \"mug\", \"hat\", \"book\", or \"keychain\"). Each product name was added to the `customer_orders` set.\n", + "\n", + "- This time, instead of asking for exactly three products, we improve the process as follows:\n", + "\n", + " 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 chooses not to add another product.\n", + "\n", + "> **Note:** \n", + "> With this optimization, users can add as many products as they want, as long as they exist in the predefined `products` list. \n", + "> This makes the program more flexible, realistic, and user-friendly — closer to a real online store experience. \n", + ">\n", + "> This corresponds to **Exercise 2 in the current \"Flow Control\" lab**.\n" + ] + }, + { + "cell_type": "code", + "execution_count": 46, + "id": "3b01a11e", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Here are the available products : ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n", + "\n", + "Current Customer's orders: set()\n", + "- book has been added to your order!\n", + "- hat has been added to your order!\n", + "- mug has been added to your order!\n", + "- t-shirt has been added to your order!\n", + "\n", + "Updated Customer's orders: {'t-shirt', 'book', 'hat', 'mug'}\n" + ] + } + ], + "source": [ + "print(\"Here are the available products :\", products)\n", + "print(\"\\nCurrent Customer's orders:\", customer_orders)\n", + "\n", + "# Here we optimize our previous code by using a while loop instead of asking for a fixed number of products.\n", + "# Making program more flexible and user-friendly.\n", + "while True:\n", + " # 2a. Prompt the user to enter the name of a product\n", + " product_name = input(\"Enter the name of a product to order: \").strip().lower()\n", + "\n", + " # 2b. Add the product name to the \"customer_orders\" set (validate first)\n", + " # Improving input validation and enhances the user experience with clearer messages.\n", + " if product_name in products:\n", + " customer_orders.add(product_name)\n", + " print(f\"- {product_name} has been added to your order!\")\n", + " else:\n", + " print(\"The product you entered was not found. Please verify that the name is spelled correctly and select from the available options:\", products)\n", + "\n", + " # 2c. Ask the user if they want to add another product (yes/no)\n", + " # Allowing the customer to add as many products as they want.\n", + " continue_order = input(\"Do you want to add another product? (yes/no): \").strip().lower()\n", + "\n", + " # 2d. Continue the loop until the user does not want to add another product\n", + " if continue_order not in (\"yes\", \"y\", \"si\", \"sí\"):\n", + " break\n", + "\n", + "print(\"\\nUpdated Customer's orders:\", customer_orders)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "id": "cd848da3", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Here are the available products : ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n", + "\n", + "Current Customer's orders: {'t-shirt', 'keychain', 'hat', 'mug'}\n", + "- book has been added to your order!\n", + "\n", + "Updated Customer's orders:\n", + "- keychain\n", + "- mug\n", + "- hat\n", + "- t-shirt\n", + "- book\n" + ] + } + ], + "source": [ + "# Additional optimized version with vertical print of customer orders\n", + "\n", + "print(\"Here are the available products :\", products)\n", + "print(\"\\nCurrent Customer's orders:\", customer_orders)\n", + "\n", + "# Here we optimize our previous code by using a while loop instead of asking for a fixed number of products.\n", + "# Making program more flexible and user-friendly.\n", + "while True:\n", + " # 2a. Prompt the user to enter the name of a product\n", + " product_name = input(\"Enter the name of a product to order: \").strip().lower()\n", + "\n", + " # 2b. Add the product name to the \"customer_orders\" set (validate first)\n", + " # Improving input validation and enhances the user experience with clearer messages.\n", + " if product_name in products:\n", + " customer_orders.add(product_name)\n", + " print(f\"- {product_name} has been added to your order!\")\n", + " else:\n", + " print(\"The product you entered was not found. Please verify that the name is spelled correctly and select from the available options:\", products)\n", + "\n", + " # 2c. Ask the user if they want to add another product (yes/no)\n", + " # Allowing the customer to add as many products as they want.\n", + " continue_order = input(\"Do you want to add another product? (yes/no): \").strip().lower()\n", + "\n", + " # 2d. Continue the loop until the user does not want to add another product\n", + " if continue_order not in (\"yes\", \"y\", \"si\", \"sí\"):\n", + " break\n", + "\n", + "# Print customer orders vertically instead of horizontally\n", + "print(\"\\nUpdated Customer's orders:\")\n", + "for product in customer_orders:\n", + " print(f\"- {product}\")\n" + ] + }, + { + "cell_type": "markdown", + "id": "1dcfe592", + "metadata": {}, + "source": [ + "-----\n", + "6. Print the products in the `customer_orders` set. Code from lab *Data Structures*\n", + "\n", + "> **Note:** \n", + "> Notice that even though we are using the code from the *Data Structures* lab, we are now able to add **more than three products** to our updated `customer_orders` set, \n", + "> thanks to the new loop implemented in Exercise 2 of this lab.\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 48, + "id": "d7b9bdbd", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Updated Customer's orders: {'t-shirt', 'book', 'hat', 'mug'}\n", + "- t-shirt\n", + "- book\n", + "- hat\n", + "- mug\n" + ] + } + ], + "source": [ + "# 6. printing the Updated Customer's orders: ----------------------------------------------------------------\n", + "print(\"\\nUpdated Customer's orders:\", customer_orders)\n", + "\n", + "# We only omptimize by printing each product on a new line vertically instead of horizontally as before.\n", + "for product in customer_orders:\n", + " print(f\"- {product}\")" + ] + }, + { + "cell_type": "markdown", + "id": "58a741c3", + "metadata": {}, + "source": [ + "-----\n", + "7. Calculate the following order statistics:\n", + " - Total Products Ordered: The total number of products in the `customer_orders` set.\n", + " - Percentage of Products Ordered: The percentage of products ordered compared to the total available products.\n", + " \n", + " > Store these statistics in a tuple called `order_status`.\n", + " > Code from the *Data Structures* lab." + ] + }, + { + "cell_type": "code", + "execution_count": 49, + "id": "5655263e", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Total Products Ordered: 4\n", + "Percentage of Products Ordered: 80.00%\n", + "\n", + "Order Status (Total Products Ordered, Percentage): (4, 80.0)\n" + ] + } + ], + "source": [ + "# 7. Calculate the following order statistics: ------------------------------------------------------------\n", + "# 7.1. Total products ordered\n", + "\n", + "total_products_ordered = len(customer_orders)\n", + "print(f\"\\nTotal Products Ordered: {total_products_ordered}\")\n", + "\n", + "# 7.2. Percentage products ordered from total products ordered\n", + "percentage = total_products_ordered / len(products) * 100\n", + "\n", + "print(f\"Percentage of Products Ordered: {percentage:.2f}%\")\n", + "\n", + "# Store these statistics in a tuple called `order_status`.\n", + "\n", + "order_status = (total_products_ordered, percentage)\n", + "print(\"\\nOrder Status (Total Products Ordered, Percentage):\", order_status)" + ] + }, + { + "cell_type": "markdown", + "id": "d0f3296b", + "metadata": {}, + "source": [ + "-----\n", + "8. Print the order statistics using the following format:\n", + " ```\n", + " Order Statistics:\n", + " Total Products Ordered: \n", + " Percentage of Products Ordered: % \n", + " ```\n", + " > Code from the *Data Structures* lab." + ] + }, + { + "cell_type": "code", + "execution_count": 50, + "id": "94b35179", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Order Statistics:\n", + "Total Products Ordered: 4\n", + "Percentage of Products Ordered: 80.00% \n" + ] + } + ], + "source": [ + "# Print the order statistics using the following format:\n", + "\n", + "# Order Statistics:\n", + "print(\"\\nOrder Statistics:\")\n", + "\n", + "# Total Products Ordered: \n", + "print(f\"Total Products Ordered: {total_products_ordered}\")\n", + "\n", + "# Percentage of Products Ordered: % \n", + "print(f\"Percentage of Products Ordered: {percentage:.2f}% \")" + ] + }, + { + "cell_type": "markdown", + "id": "761f080a", + "metadata": {}, + "source": [ + "-----\n", + "9. Update the inventory by subtracting 1 from the quantity of each product. Modify the `inventory` dictionary accordingly.\n", + "\n", + "> Code from the *Data Structures* lab." + ] + }, + { + "cell_type": "code", + "execution_count": 51, + "id": "d8fb3dad", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "- t-shirt inventory decreased by 1. New quantity: 9\n", + "- book inventory decreased by 1. New quantity: 9\n", + "- hat inventory decreased by 1. New quantity: 9\n", + "- mug inventory decreased by 1. New quantity: 9\n" + ] + } + ], + "source": [ + "# 9. Update the inventory dict by substracting 1 from products in customer_orders set ---------------------------------------------\n", + "for product in customer_orders:\n", + " if inventory[product] > 0:\n", + " inventory[product] -= 1\n", + " print(f\"- {product} inventory decreased by 1. New quantity: {inventory[product]}\")\n", + " else:\n", + " print(f\"- {product} is out of stock!\")\n" + ] + }, + { + "cell_type": "markdown", + "id": "8110f87c", + "metadata": {}, + "source": [ + "-----\n", + "10. Print the updated inventory, displaying the quantity of each product on separate lines.\n", + " > Code from the *Data Structures* lab." + ] + }, + { + "cell_type": "code", + "execution_count": 52, + "id": "4385e0dd", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Updated Inventory after Customer's orders: {'t-shirt': 9, 'mug': 9, 'hat': 9, 'book': 9, 'keychain': 10}\n", + "- t-shirt: 9 units left\n", + "- mug: 9 units left\n", + "- hat: 9 units left\n", + "- book: 9 units left\n", + "- keychain: 10 units left\n" + ] + } + ], + "source": [ + "# Print the updated inventory\n", + "print(\"\\nUpdated Inventory after Customer's orders:\", inventory)\n", + "for product in products:\n", + " print(f\"- {product}: {inventory[product]} units left\")\n", + " " + ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, @@ -55,7 +585,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.5" } }, "nbformat": 4, From 8a6d605e66a1f114da3230effb098a0c291b3960 Mon Sep 17 00:00:00 2001 From: PaulinaMamiaga <228375023+PaulinaMamiaga@users.noreply.github.com> Date: Thu, 6 Nov 2025 11:03:39 +0100 Subject: [PATCH 2/2] Updated lab Python Flow Control / 06.11.25 / 11:03 am --- lab-python-flow-control.ipynb | 59 +++++++++++++++++++---------------- 1 file changed, 32 insertions(+), 27 deletions(-) diff --git a/lab-python-flow-control.ipynb b/lab-python-flow-control.ipynb index 2eaa0b0..b1b1df4 100644 --- a/lab-python-flow-control.ipynb +++ b/lab-python-flow-control.ipynb @@ -299,7 +299,7 @@ }, { "cell_type": "code", - "execution_count": 36, + "execution_count": 54, "id": "cd848da3", "metadata": {}, "outputs": [ @@ -309,8 +309,11 @@ "text": [ "Here are the available products : ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n", "\n", - "Current Customer's orders: {'t-shirt', 'keychain', 'hat', 'mug'}\n", + "Current Customer's orders: {'t-shirt', 'book', 'hat', 'mug'}\n", + "- mug has been added to your order!\n", "- book has been added to your order!\n", + "- t-shirt has been added to your order!\n", + "- keychain has been added to your order!\n", "\n", "Updated Customer's orders:\n", "- keychain\n", @@ -346,7 +349,7 @@ " continue_order = input(\"Do you want to add another product? (yes/no): \").strip().lower()\n", "\n", " # 2d. Continue the loop until the user does not want to add another product\n", - " if continue_order not in (\"yes\", \"y\", \"si\", \"sí\"):\n", + " if continue_order not in (\"yes\", \"y\"):\n", " break\n", "\n", "# Print customer orders vertically instead of horizontally\n", @@ -371,7 +374,7 @@ }, { "cell_type": "code", - "execution_count": 48, + "execution_count": 55, "id": "d7b9bdbd", "metadata": {}, "outputs": [ @@ -380,11 +383,12 @@ "output_type": "stream", "text": [ "\n", - "Updated Customer's orders: {'t-shirt', 'book', 'hat', 'mug'}\n", - "- t-shirt\n", - "- book\n", + "Updated Customer's orders: {'keychain', 'mug', 'hat', 't-shirt', 'book'}\n", + "- keychain\n", + "- mug\n", "- hat\n", - "- mug\n" + "- t-shirt\n", + "- book\n" ] } ], @@ -413,7 +417,7 @@ }, { "cell_type": "code", - "execution_count": 49, + "execution_count": 56, "id": "5655263e", "metadata": {}, "outputs": [ @@ -422,10 +426,10 @@ "output_type": "stream", "text": [ "\n", - "Total Products Ordered: 4\n", - "Percentage of Products Ordered: 80.00%\n", + "Total Products Ordered: 5\n", + "Percentage of Products Ordered: 100.00%\n", "\n", - "Order Status (Total Products Ordered, Percentage): (4, 80.0)\n" + "Order Status (Total Products Ordered, Percentage): (5, 100.0)\n" ] } ], @@ -464,7 +468,7 @@ }, { "cell_type": "code", - "execution_count": 50, + "execution_count": 57, "id": "94b35179", "metadata": {}, "outputs": [ @@ -474,8 +478,8 @@ "text": [ "\n", "Order Statistics:\n", - "Total Products Ordered: 4\n", - "Percentage of Products Ordered: 80.00% \n" + "Total Products Ordered: 5\n", + "Percentage of Products Ordered: 100.00% \n" ] } ], @@ -505,7 +509,7 @@ }, { "cell_type": "code", - "execution_count": 51, + "execution_count": 58, "id": "d8fb3dad", "metadata": {}, "outputs": [ @@ -513,10 +517,11 @@ "name": "stdout", "output_type": "stream", "text": [ - "- t-shirt inventory decreased by 1. New quantity: 9\n", - "- book inventory decreased by 1. New quantity: 9\n", - "- hat inventory decreased by 1. New quantity: 9\n", - "- mug inventory decreased by 1. New quantity: 9\n" + "- keychain inventory decreased by 1. New quantity: 9\n", + "- mug inventory decreased by 1. New quantity: 8\n", + "- hat inventory decreased by 1. New quantity: 8\n", + "- t-shirt inventory decreased by 1. New quantity: 8\n", + "- book inventory decreased by 1. New quantity: 8\n" ] } ], @@ -542,7 +547,7 @@ }, { "cell_type": "code", - "execution_count": 52, + "execution_count": 59, "id": "4385e0dd", "metadata": {}, "outputs": [ @@ -551,12 +556,12 @@ "output_type": "stream", "text": [ "\n", - "Updated Inventory after Customer's orders: {'t-shirt': 9, 'mug': 9, 'hat': 9, 'book': 9, 'keychain': 10}\n", - "- t-shirt: 9 units left\n", - "- mug: 9 units left\n", - "- hat: 9 units left\n", - "- book: 9 units left\n", - "- keychain: 10 units left\n" + "Updated Inventory after Customer's orders: {'t-shirt': 8, 'mug': 8, 'hat': 8, 'book': 8, 'keychain': 9}\n", + "- t-shirt: 8 units left\n", + "- mug: 8 units left\n", + "- hat: 8 units left\n", + "- book: 8 units left\n", + "- keychain: 9 units left\n" ] } ],