From 6d50c9284e6509b2acc64e60e803caa61a854bee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alberto=20Marqu=C3=A9s=20Cabedo?= Date: Tue, 14 Apr 2026 13:46:49 +0200 Subject: [PATCH 1/5] Update lab-python-flow-control.ipynb Improving user experience --- lab-python-flow-control.ipynb | 78 ++++++++++++++++++++++++++++++++++- 1 file changed, 76 insertions(+), 2 deletions(-) diff --git a/lab-python-flow-control.ipynb b/lab-python-flow-control.ipynb index f4c7391..37f17f6 100644 --- a/lab-python-flow-control.ipynb +++ b/lab-python-flow-control.ipynb @@ -37,11 +37,85 @@ "\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": 3, + "id": "0da27de0", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Type 'Exit' at any time to stop purchasing\n", + "Available prodcuts ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n", + "The customer has ordered {'mug', 'book'}\n", + "\n", + "Order Statistics:\n", + " Total Products Ordered: 2\n", + " Percentage of Products Ordered: 40.0%\n", + "\n", + "The actual quantity of item t-shirt is: 1\n", + "The actual quantity of item mug is: 0\n", + "The actual quantity of item hat is: 1\n", + "The actual quantity of item book is: 0\n", + "The actual quantity of item keychain is: 1\n" + ] + } + ], + "source": [ + "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "print (\"Type 'Exit' at any time to stop purchasing\")\n", + "print (\"Available prodcuts\", products)\n", + "\n", + "inventory = {}\n", + "for product in products:\n", + " quantity = int(input(f\"Enter quantity of {product}: \"))\n", + " inventory[product] = quantity\n", + "\n", + "customer_orders = set()\n", + "\n", + "while True:\n", + " order = input('Enter the name of the products to purchase: ').lower()\n", + " \n", + " if order in products:\n", + " customer_orders.add(order)\n", + " inventory[order] -= 1\n", + "\n", + " if order == \"exit\":\n", + " break\n", + "\n", + "print (f\"The customer has ordered {customer_orders}\" \"\\n\")\n", + "\n", + "total_products_ordered = len(customer_orders)\n", + "percentage_ordered = (total_products_ordered/len(products))*100\n", + "\n", + "order_status = (total_products_ordered, percentage_ordered)\n", + "\n", + "print (\"Order Statistics:\" \"\\n\", \n", + "f\"Total Products Ordered: {order_status[0]}\" \"\\n\",\n", + "f\"Percentage of Products Ordered: {order_status[1]}%\" \"\\n\")\n", + "\n", + "# for order in customer_orders:\n", + "# inventory[order] -= 1 \n", + "\n", + "for key,value in inventory.items():\n", + " print (f\"The actual quantity of item {key} is: {value}\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "51ccf0e7", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -55,7 +129,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.14.4" } }, "nbformat": 4, From 3771ea51e5e66e25e9183806eee67b8d9e32484f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alberto=20Marqu=C3=A9s=20Cabedo?= Date: Tue, 14 Apr 2026 14:15:29 +0200 Subject: [PATCH 2/5] Update lab-python-flow-control.ipynb Add input validation --- lab-python-flow-control.ipynb | 35 ++++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/lab-python-flow-control.ipynb b/lab-python-flow-control.ipynb index 37f17f6..a23d837 100644 --- a/lab-python-flow-control.ipynb +++ b/lab-python-flow-control.ipynb @@ -40,7 +40,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": null, "id": "0da27de0", "metadata": {}, "outputs": [ @@ -48,25 +48,24 @@ "name": "stdout", "output_type": "stream", "text": [ - "Type 'Exit' at any time to stop purchasing\n", "Available prodcuts ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n", - "The customer has ordered {'mug', 'book'}\n", + "The customer has ordered {'book', 'mug'}\n", "\n", "Order Statistics:\n", " Total Products Ordered: 2\n", " Percentage of Products Ordered: 40.0%\n", "\n", - "The actual quantity of item t-shirt is: 1\n", - "The actual quantity of item mug is: 0\n", - "The actual quantity of item hat is: 1\n", - "The actual quantity of item book is: 0\n", - "The actual quantity of item keychain is: 1\n" + "The actual quantity of item t-shirt is: 34\n", + "The actual quantity of item mug is: 64\n", + "The actual quantity of item hat is: 23\n", + "The actual quantity of item book is: 66\n", + "The actual quantity of item keychain is: 445\n" ] } ], "source": [ "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", - "print (\"Type 'Exit' at any time to stop purchasing\")\n", + "# print (\"Type 'Exit' at any time to stop purchasing\")\n", "print (\"Available prodcuts\", products)\n", "\n", "inventory = {}\n", @@ -77,13 +76,14 @@ "customer_orders = set()\n", "\n", "while True:\n", - " order = input('Enter the name of the products to purchase: ').lower()\n", + " order = input('Enter the name of the product to purchase: ').lower()\n", " \n", " if order in products:\n", " customer_orders.add(order)\n", " inventory[order] -= 1\n", "\n", - " if order == \"exit\":\n", + " validation = input(\"Do you want buy more items? (yes/no): \")\n", + " if validation == \"no\":\n", " break\n", "\n", "print (f\"The customer has ordered {customer_orders}\" \"\\n\")\n", @@ -95,15 +95,20 @@ "\n", "print (\"Order Statistics:\" \"\\n\", \n", "f\"Total Products Ordered: {order_status[0]}\" \"\\n\",\n", - "f\"Percentage of Products Ordered: {order_status[1]}%\" \"\\n\")\n", - "\n", - "# for order in customer_orders:\n", - "# inventory[order] -= 1 \n", + "f\"Percentage of Products Ordered : {order_status[1]}%\" \"\\n\")\n", "\n", "for key,value in inventory.items():\n", " print (f\"The actual quantity of item {key} is: {value}\")" ] }, + { + "cell_type": "code", + "execution_count": null, + "id": "8ca75570", + "metadata": {}, + "outputs": [], + "source": [] + }, { "cell_type": "code", "execution_count": null, From 3cab5f671e6a2d0ca6ac222c377c692160f87581 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alberto=20Marqu=C3=A9s=20Cabedo?= Date: Tue, 14 Apr 2026 15:03:45 +0200 Subject: [PATCH 3/5] Update lab-python-flow-control.ipynb Improve user experience --- lab-python-flow-control.ipynb | 44 +++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 20 deletions(-) diff --git a/lab-python-flow-control.ipynb b/lab-python-flow-control.ipynb index a23d837..bb69338 100644 --- a/lab-python-flow-control.ipynb +++ b/lab-python-flow-control.ipynb @@ -40,7 +40,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "id": "0da27de0", "metadata": {}, "outputs": [ @@ -48,25 +48,27 @@ "name": "stdout", "output_type": "stream", "text": [ - "Available prodcuts ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n", - "The customer has ordered {'book', 'mug'}\n", + "Available products ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n", + "fg is not a correct option. Please select 'yes' or 'no'\n", + "h is not a correct option. Please select 'yes' or 'no'\n", + "The customer has ordered {'hat', 'book'}\n", "\n", "Order Statistics:\n", " Total Products Ordered: 2\n", - " Percentage of Products Ordered: 40.0%\n", + " Percentage of Products Ordered : 40.0%\n", "\n", - "The actual quantity of item t-shirt is: 34\n", - "The actual quantity of item mug is: 64\n", - "The actual quantity of item hat is: 23\n", - "The actual quantity of item book is: 66\n", - "The actual quantity of item keychain is: 445\n" + "The actual quantity of item t-shirt is: 24\n", + "The actual quantity of item mug is: 34\n", + "The actual quantity of item hat is: 22\n", + "The actual quantity of item book is: 33\n", + "The actual quantity of item keychain is: 34\n" ] } ], "source": [ "products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", "# print (\"Type 'Exit' at any time to stop purchasing\")\n", - "print (\"Available prodcuts\", products)\n", + "print (\"Available products\", products)\n", "\n", "inventory = {}\n", "for product in products:\n", @@ -81,10 +83,20 @@ " if order in products:\n", " customer_orders.add(order)\n", " inventory[order] -= 1\n", + " else:\n", + " while order not in products:\n", + " print (f\"{order} is not in our inventory. Please select another item\")\n", + " order = input('Enter the name of the product to purchase: ').lower()\n", + "\n", + " validation = input(\"Do you want. to buy more items? (yes/no): \").lower()\n", + "\n", + " while validation != \"no\" and validation != \"yes\":\n", + " print(f\"{validation} is not a correct option. Please select 'yes' or 'no'\")\n", + " validation = input(\"Do you want. to buy more items? (yes/no): \").lower()\n", "\n", - " validation = input(\"Do you want buy more items? (yes/no): \")\n", " if validation == \"no\":\n", " break\n", + " \n", "\n", "print (f\"The customer has ordered {customer_orders}\" \"\\n\")\n", "\n", @@ -104,15 +116,7 @@ { "cell_type": "code", "execution_count": null, - "id": "8ca75570", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "51ccf0e7", + "id": "fa6bbaa8", "metadata": {}, "outputs": [], "source": [] From 9f7675d1820a0a531e05316addd7e39a6b358a53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alberto=20Marqu=C3=A9s=20Cabedo?= Date: Tue, 14 Apr 2026 15:37:34 +0200 Subject: [PATCH 4/5] Update lab-python-flow-control.ipynb Move inventory update outside the loop and improve validation readability --- lab-python-flow-control.ipynb | 48 +++++++++++++++++------------------ 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/lab-python-flow-control.ipynb b/lab-python-flow-control.ipynb index bb69338..964f76a 100644 --- a/lab-python-flow-control.ipynb +++ b/lab-python-flow-control.ipynb @@ -40,7 +40,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": null, "id": "0da27de0", "metadata": {}, "outputs": [ @@ -48,20 +48,7 @@ "name": "stdout", "output_type": "stream", "text": [ - "Available products ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n", - "fg is not a correct option. Please select 'yes' or 'no'\n", - "h is not a correct option. Please select 'yes' or 'no'\n", - "The customer has ordered {'hat', 'book'}\n", - "\n", - "Order Statistics:\n", - " Total Products Ordered: 2\n", - " Percentage of Products Ordered : 40.0%\n", - "\n", - "The actual quantity of item t-shirt is: 24\n", - "The actual quantity of item mug is: 34\n", - "The actual quantity of item hat is: 22\n", - "The actual quantity of item book is: 33\n", - "The actual quantity of item keychain is: 34\n" + "Available products ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n" ] } ], @@ -72,27 +59,29 @@ "\n", "inventory = {}\n", "for product in products:\n", - " quantity = int(input(f\"Enter quantity of {product}: \"))\n", + " quantity = input(f\"Enter quantity of {product}: \").strip()\n", " inventory[product] = quantity\n", "\n", "customer_orders = set()\n", "\n", "while True:\n", - " order = input('Enter the name of the product to purchase: ').lower()\n", + " order = input('Enter the name of the product to purchase: ').strip().lower()\n", " \n", " if order in products:\n", " customer_orders.add(order)\n", - " inventory[order] -= 1\n", + " \n", " else:\n", " while order not in products:\n", - " print (f\"{order} is not in our inventory. Please select another item\")\n", - " order = input('Enter the name of the product to purchase: ').lower()\n", + " print (f\"'{order}' is not in our inventory. Please select another item.\")\n", + " order = input('Enter the name of the product to purchase: ').strip().lower()\n", + " if order in products:\n", + " customer_orders.add(order)\n", "\n", - " validation = input(\"Do you want. to buy more items? (yes/no): \").lower()\n", + " validation = input(\"Do you want. to buy more items? (yes/no): \").strip().lower()\n", "\n", - " while validation != \"no\" and validation != \"yes\":\n", - " print(f\"{validation} is not a correct option. Please select 'yes' or 'no'\")\n", - " validation = input(\"Do you want. to buy more items? (yes/no): \").lower()\n", + " while validation not in (\"no\", \"yes\"):\n", + " print(f\"'{validation}' is not a correct option. Please select 'yes' or 'no'.\")\n", + " validation = input(\"Do you want to buy more items? (yes/no): \").strip().lower()\n", "\n", " if validation == \"no\":\n", " break\n", @@ -109,6 +98,9 @@ "f\"Total Products Ordered: {order_status[0]}\" \"\\n\",\n", "f\"Percentage of Products Ordered : {order_status[1]}%\" \"\\n\")\n", "\n", + "for item in customer_orders:\n", + " inventory[item] -= 1\n", + "\n", "for key,value in inventory.items():\n", " print (f\"The actual quantity of item {key} is: {value}\")" ] @@ -120,6 +112,14 @@ "metadata": {}, "outputs": [], "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "802b1e0f", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { From 9b0dcae9ab47891a93bb70e3e01484faec40156d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alberto=20Marqu=C3=A9s=20Cabedo?= Date: Tue, 14 Apr 2026 15:44:33 +0200 Subject: [PATCH 5/5] Update lab-python-flow-control.ipynb improve readability --- lab-python-flow-control.ipynb | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/lab-python-flow-control.ipynb b/lab-python-flow-control.ipynb index 964f76a..e0075ba 100644 --- a/lab-python-flow-control.ipynb +++ b/lab-python-flow-control.ipynb @@ -40,7 +40,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "id": "0da27de0", "metadata": {}, "outputs": [ @@ -48,7 +48,20 @@ "name": "stdout", "output_type": "stream", "text": [ - "Available products ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n" + "Available products ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n", + "'mu' is not in our inventory. Please select another item.\n", + "'mu' is not in our inventory. Please select another item.\n", + "The customer has ordered {'mug', 'book'}\n", + "\n", + "Order Statistics:\n", + " Total Products Ordered: 2\n", + " Percentage of Products Ordered : 40.0%\n", + "\n", + "The actual quantity of item t-shirt is: 1\n", + "The actual quantity of item mug is: 0\n", + "The actual quantity of item hat is: 1\n", + "The actual quantity of item book is: 0\n", + "The actual quantity of item keychain is: 1\n" ] } ], @@ -59,7 +72,7 @@ "\n", "inventory = {}\n", "for product in products:\n", - " quantity = input(f\"Enter quantity of {product}: \").strip()\n", + " quantity = int(input(f\"Enter quantity of {product}: \").strip())\n", " inventory[product] = quantity\n", "\n", "customer_orders = set()\n", @@ -77,7 +90,7 @@ " if order in products:\n", " customer_orders.add(order)\n", "\n", - " validation = input(\"Do you want. to buy more items? (yes/no): \").strip().lower()\n", + " validation = input(\"Do you want to buy more items? (yes/no): \").strip().lower()\n", "\n", " while validation not in (\"no\", \"yes\"):\n", " print(f\"'{validation}' is not a correct option. Please select 'yes' or 'no'.\")\n",