From 42818c9923b33aa0c8b23060b4b50c06dab3daa0 Mon Sep 17 00:00:00 2001 From: nailaikhenazenni-afk Date: Fri, 17 Apr 2026 22:04:49 +0200 Subject: [PATCH 01/20] Update lab-python-error-handling.ipynb --- lab-python-error-handling.ipynb | 79 ++++++++++++++++++++++++++++++++- 1 file changed, 77 insertions(+), 2 deletions(-) diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index f4c6ef6..67f53db 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -72,11 +72,86 @@ "\n", "4. Test your code by running the program and deliberately entering invalid quantities and product names. Make sure the error handling mechanism works as expected.\n" ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "b4154ae6", + "metadata": {}, + "outputs": [], + "source": [ + "def calculate_total_price(customer_orders):\n", + " \n", + " total_price= sum(\n", + " float(input(f\"Enter the price of {product}: \")) \n", + " for product in customer_orders\n", + " \n", + " \n", + " )\n", + " try:\n", + " total_price= sum(\n", + " float(input(f\"Enter the price of {product}: \")) \n", + " for product in customer_orders\n", + " )\n", + " except ValueError:\n", + " print('Error re-enter the price for that product.')\n", + " return calculate_total_price(customer_orders)\n", + " \n", + "\n", + "\n", + " return total_price\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c568692b", + "metadata": {}, + "outputs": [], + "source": [ + "def get_customer_orders(products):\n", + " \n", + " try:\n", + " orders_count = int(input(\"Enter the number of customer orders: \"))\n", + " except ValueError:\n", + " print(\"Error: Please enter a valid integer for the number of orders.\")\n", + " return set()\n", + "\n", + " customer_orders = set()\n", + "\n", + " for i in range(orders_count):\n", + " while True:\n", + " try:\n", + " product_name = input(f\"Gather the name for product {i + 1}: \")\n", + " \n", + " # Si el producto no está en la lista, lanzamos un ValueError manualmente\n", + " if product_name not in products:\n", + " raise ValueError(f\"'{product_name}' is not in the catalog.\")\n", + " \n", + " # Si no hubo error, lo añadimos y rompemos el bucle while\n", + " customer_orders.add(product_name)\n", + " break\n", + " \n", + " except ValueError as e:\n", + " # Aquí capturamos tanto errores de lógica como el mensaje personalizado\n", + " print(f\"Invalid input: {e} Please try again.\")\n", + "\n", + " return customer_orders\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "28152b2c", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, @@ -90,7 +165,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.9" } }, "nbformat": 4, From 7a4ae5ac7d65e274147fabb8bb681c85c0688211 Mon Sep 17 00:00:00 2001 From: nailaikhenazenni-afk Date: Fri, 17 Apr 2026 23:01:33 +0200 Subject: [PATCH 02/20] Update lab-python-error-handling.ipynb --- lab-python-error-handling.ipynb | 83 ++++++++++++++++++++++++++------- 1 file changed, 66 insertions(+), 17 deletions(-) diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index 67f53db..4f1a26f 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -75,19 +75,37 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 8, + "id": "464b1353", + "metadata": {}, + "outputs": [], + "source": [ + "def initialize_inventory(products):\n", + " inventory = {}\n", + " for product in products:\n", + " valid_input = False\n", + " while not valid_input:\n", + " try:\n", + " quantity = int(input(f\"Enter the quantity of {product}s available: \"))\n", + " if quantity >= 0:\n", + " inventory[product] = quantity\n", + " valid_input = True\n", + " else:\n", + " print(\"Quantity cannot be negative. Please enter a valid quantity.\")\n", + " except ValueError:\n", + " print(\"Invalid input. Please enter a valid quantity.\")\n", + " return inventory" + ] + }, + { + "cell_type": "code", + "execution_count": 10, "id": "b4154ae6", "metadata": {}, "outputs": [], "source": [ "def calculate_total_price(customer_orders):\n", " \n", - " total_price= sum(\n", - " float(input(f\"Enter the price of {product}: \")) \n", - " for product in customer_orders\n", - " \n", - " \n", - " )\n", " try:\n", " total_price= sum(\n", " float(input(f\"Enter the price of {product}: \")) \n", @@ -104,36 +122,36 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "id": "c568692b", "metadata": {}, "outputs": [], "source": [ - "def get_customer_orders(products):\n", + "def get_customer_orders( inventory):\n", " \n", " try:\n", " orders_count = int(input(\"Enter the number of customer orders: \"))\n", " except ValueError:\n", - " print(\"Error: Please enter a valid integer for the number of orders.\")\n", + " print(\"Error: Please enter a not negative integer for the number of orders.\")\n", " return set()\n", "\n", " customer_orders = set()\n", "\n", - " for i in range(orders_count):\n", + " for orders in range(orders_count):\n", " while True:\n", " try:\n", - " product_name = input(f\"Gather the name for product {i + 1}: \")\n", + " product_name = input(f\"Gather the name for product: \")\n", " \n", - " # Si el producto no está en la lista, lanzamos un ValueError manualmente\n", - " if product_name not in products:\n", - " raise ValueError(f\"'{product_name}' is not in the catalog.\")\n", " \n", - " # Si no hubo error, lo añadimos y rompemos el bucle while\n", + " if product_name not in inventory:\n", + " raise ValueError(f\"'{product_name}' is not in the inventory.\")\n", + " \n", + " \n", " customer_orders.add(product_name)\n", " break\n", " \n", " except ValueError as e:\n", - " # Aquí capturamos tanto errores de lógica como el mensaje personalizado\n", + " \n", " print(f\"Invalid input: {e} Please try again.\")\n", "\n", " return customer_orders\n", @@ -145,6 +163,37 @@ "execution_count": null, "id": "28152b2c", "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "--- Customer Orders ---\n", + "Inventory: {'hat', 'mug', 'tshirt', 'keychain', 'book'}\n", + "------------------------------\n" + ] + } + ], + "source": [ + "if __name__ == \"__main__\":\n", + " \n", + " inventory= {\"hat\", \"mug\", \"keychain\", \"tshirt\", \"book\"}\n", + "\n", + " print(\"--- Customer Orders ---\")\n", + " print(f\"Inventory: {inventory}\")\n", + " print(\"-\" * 30)\n", + "\n", + " customer_orders = get_customer_orders(inventory)\n", + "\n", + " print(\"-\" * 30)\n", + " print(f\"Customer Orders: {customer_orders}\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "fa19394e", + "metadata": {}, "outputs": [], "source": [] } From 735525285552eb60208de598a0b8309cdf54b7b0 Mon Sep 17 00:00:00 2001 From: nailaikhenazenni-afk Date: Fri, 17 Apr 2026 23:51:03 +0200 Subject: [PATCH 03/20] Update lab-python-error-handling.ipynb --- lab-python-error-handling.ipynb | 105 +++++++++++++++----------------- 1 file changed, 48 insertions(+), 57 deletions(-) diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index 4f1a26f..827f5dc 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -75,54 +75,76 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 23, "id": "464b1353", "metadata": {}, - "outputs": [], + "outputs": [ + { + "ename": "SyntaxError", + "evalue": "invalid syntax (2180418459.py, line 5)", + "output_type": "error", + "traceback": [ + " \u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[23]\u001b[39m\u001b[32m, line 5\u001b[39m\n\u001b[31m \u001b[39m\u001b[31mwhile nt valid_quantity:\u001b[39m\n ^\n\u001b[31mSyntaxError\u001b[39m\u001b[31m:\u001b[39m invalid syntax\n" + ] + } + ], "source": [ "def initialize_inventory(products):\n", " inventory = {}\n", " for product in products:\n", - " valid_input = False\n", - " while not valid_input:\n", + " valid_quantity = False\n", + " while nt valid_quantity:\n", " try:\n", " quantity = int(input(f\"Enter the quantity of {product}s available: \"))\n", - " if quantity >= 0:\n", - " inventory[product] = quantity\n", - " valid_input = True\n", - " else:\n", - " print(\"Quantity cannot be negative. Please enter a valid quantity.\")\n", - " except ValueError:\n", - " print(\"Invalid input. Please enter a valid quantity.\")\n", + " if quantity < 0:\n", + " raise ValueError(\"Invalid quantity! Please enter a non-negative value.\")\n", + " valid_quantity = True\n", + " except ValueError as error:\n", + " print(f\"Error: {error}\")\n", + " inventory[product] = quantity\n", " return inventory" ] }, { "cell_type": "code", - "execution_count": 10, + "execution_count": null, "id": "b4154ae6", "metadata": {}, "outputs": [], "source": [ "def calculate_total_price(customer_orders):\n", - " \n", - " try:\n", - " total_price= sum(\n", - " float(input(f\"Enter the price of {product}: \")) \n", - " for product in customer_orders\n", - " )\n", - " except ValueError:\n", - " print('Error re-enter the price for that product.')\n", - " return calculate_total_price(customer_orders)\n", - " \n", - "\n", + " \n", + " total_price = 0.0\n", "\n", - " return total_price\n" + " for product in customer_orders:\n", + " valid_input = False\n", + " \n", + " while not valid_input:\n", + " try:\n", + " \n", + " price = float(input(f\"Enter the price of {product}: \"))\n", + " \n", + " \n", + " if price < 0:\n", + " raise ValueError(\"Negative values are not allowed.\")\n", + " \n", + " # Si llega aquí, el valor es numérico y positivo.\n", + " total_price += price\n", + " valid_input = True \n", + " \n", + " except ValueError as e:\n", + " \n", + " if \"could not convert string to float\" in str(e):\n", + " print(f\"Error: Non-numeric value detected. Please enter a number for {product}.\")\n", + " else:\n", + " print(f\"Error: {e} Please re-enter the price.\")\n", + " \n", + " return total_price" ] }, { "cell_type": "code", - "execution_count": 9, + "execution_count": null, "id": "c568692b", "metadata": {}, "outputs": [], @@ -155,38 +177,7 @@ " print(f\"Invalid input: {e} Please try again.\")\n", "\n", " return customer_orders\n", - " " - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "28152b2c", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "--- Customer Orders ---\n", - "Inventory: {'hat', 'mug', 'tshirt', 'keychain', 'book'}\n", - "------------------------------\n" - ] - } - ], - "source": [ - "if __name__ == \"__main__\":\n", - " \n", - " inventory= {\"hat\", \"mug\", \"keychain\", \"tshirt\", \"book\"}\n", - "\n", - " print(\"--- Customer Orders ---\")\n", - " print(f\"Inventory: {inventory}\")\n", - " print(\"-\" * 30)\n", - "\n", - " customer_orders = get_customer_orders(inventory)\n", - "\n", - " print(\"-\" * 30)\n", - " print(f\"Customer Orders: {customer_orders}\")" + " #" ] }, { From 5cee8cbb7097154c966ba0d0a28d7865fa03465b Mon Sep 17 00:00:00 2001 From: nailaikhenazenni-afk Date: Sat, 18 Apr 2026 00:15:35 +0200 Subject: [PATCH 04/20] Update lab-python-error-handling.ipynb --- lab-python-error-handling.ipynb | 37 +++++++++++++++------------------ 1 file changed, 17 insertions(+), 20 deletions(-) diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index 827f5dc..2a61966 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -75,25 +75,16 @@ }, { "cell_type": "code", - "execution_count": 23, + "execution_count": 30, "id": "464b1353", "metadata": {}, - "outputs": [ - { - "ename": "SyntaxError", - "evalue": "invalid syntax (2180418459.py, line 5)", - "output_type": "error", - "traceback": [ - " \u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[23]\u001b[39m\u001b[32m, line 5\u001b[39m\n\u001b[31m \u001b[39m\u001b[31mwhile nt valid_quantity:\u001b[39m\n ^\n\u001b[31mSyntaxError\u001b[39m\u001b[31m:\u001b[39m invalid syntax\n" - ] - } - ], + "outputs": [], "source": [ "def initialize_inventory(products):\n", " inventory = {}\n", " for product in products:\n", " valid_quantity = False\n", - " while nt valid_quantity:\n", + " while not valid_quantity:\n", " try:\n", " quantity = int(input(f\"Enter the quantity of {product}s available: \"))\n", " if quantity < 0:\n", @@ -107,7 +98,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 31, "id": "b4154ae6", "metadata": {}, "outputs": [], @@ -128,7 +119,7 @@ " if price < 0:\n", " raise ValueError(\"Negative values are not allowed.\")\n", " \n", - " # Si llega aquí, el valor es numérico y positivo.\n", + " \n", " total_price += price\n", " valid_input = True \n", " \n", @@ -144,18 +135,23 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 29, "id": "c568692b", "metadata": {}, "outputs": [], "source": [ "def get_customer_orders( inventory):\n", " \n", + " while True:\n", " try:\n", " orders_count = int(input(\"Enter the number of customer orders: \"))\n", - " except ValueError:\n", - " print(\"Error: Please enter a not negative integer for the number of orders.\")\n", - " return set()\n", + " if orders_count < 0:\n", + " raise ValueError('Invalid number of orders! Please enter a non-negative integer.')\n", + " break\n", + " except ValueError as e:\n", + " print(f\"Error: {e}\")\n", + "\n", + " \n", "\n", " customer_orders = set()\n", "\n", @@ -166,7 +162,8 @@ " \n", " \n", " if product_name not in inventory:\n", - " raise ValueError(f\"'{product_name}' is not in the inventory.\")\n", + " raise ValueError(f\"'{product_name}' is not in the inventory or out of stock.\")\n", + " \n", " \n", " \n", " customer_orders.add(product_name)\n", @@ -177,7 +174,7 @@ " print(f\"Invalid input: {e} Please try again.\")\n", "\n", " return customer_orders\n", - " #" + " " ] }, { From efe6ea03dce2dd7c7e576fb8caa1360751235296 Mon Sep 17 00:00:00 2001 From: nailaikhenazenni-afk Date: Sat, 18 Apr 2026 00:36:29 +0200 Subject: [PATCH 05/20] Update lab-python-error-handling.ipynb --- lab-python-error-handling.ipynb | 42 +++++++++++++++------------------ 1 file changed, 19 insertions(+), 23 deletions(-) diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index 2a61966..6017f01 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -75,7 +75,7 @@ }, { "cell_type": "code", - "execution_count": 30, + "execution_count": 39, "id": "464b1353", "metadata": {}, "outputs": [], @@ -98,7 +98,7 @@ }, { "cell_type": "code", - "execution_count": 31, + "execution_count": 43, "id": "b4154ae6", "metadata": {}, "outputs": [], @@ -112,30 +112,27 @@ " \n", " while not valid_input:\n", " try:\n", - " \n", - " price = float(input(f\"Enter the price of {product}: \"))\n", + " entry = input(f\"Enter the price of {product}: \")\n", + " price = float(entry)\n", " \n", - " \n", + " # Paso 2: Validar lógica de negocio (Detecta valores negativos)\n", " if price < 0:\n", - " raise ValueError(\"Negative values are not allowed.\")\n", - " \n", - " \n", + " print(f\"Error: Negative value ({price}) is not allowed.\")\n", + " continue \n", + "\n", " total_price += price\n", - " valid_input = True \n", + " valid_input = True\n", " \n", - " except ValueError as e:\n", - " \n", - " if \"could not convert string to float\" in str(e):\n", - " print(f\"Error: Non-numeric value detected. Please enter a number for {product}.\")\n", - " else:\n", - " print(f\"Error: {e} Please re-enter the price.\")\n", + " except ValueError:\n", + "\n", + " print(f\"Error: '{entry}' is not a valid number. Please try again.\")\n", " \n", " return total_price" ] }, { "cell_type": "code", - "execution_count": 29, + "execution_count": 41, "id": "c568692b", "metadata": {}, "outputs": [], @@ -151,29 +148,28 @@ " except ValueError as e:\n", " print(f\"Error: {e}\")\n", "\n", - " \n", - "\n", " customer_orders = set()\n", "\n", " for orders in range(orders_count):\n", " while True:\n", " try:\n", - " product_name = input(f\"Gather the name for product: \")\n", - " \n", + " product_name = input(f\"Gather the name for product {orders}: \")\n", " \n", " if product_name not in inventory:\n", - " raise ValueError(f\"'{product_name}' is not in the inventory or out of stock.\")\n", + " raise ValueError(f\"'{product_name}' does not exist in our catalog.\")\n", " \n", + " if inventory[product_name] <= 0:\n", + " raise ValueError(f\"'{product_name}' is currently out of stock.\")\n", " \n", - " \n", " customer_orders.add(product_name)\n", " break\n", " \n", " except ValueError as e:\n", - " \n", " print(f\"Invalid input: {e} Please try again.\")\n", "\n", " return customer_orders\n", + " \n", + "\n", " " ] }, From 833b17983af85c966f55bd34fcf46914d45341bf Mon Sep 17 00:00:00 2001 From: nailaikhenazenni-afk Date: Sat, 18 Apr 2026 00:58:39 +0200 Subject: [PATCH 06/20] Update lab-python-error-handling.ipynb --- lab-python-error-handling.ipynb | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index 6017f01..41a281a 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -98,7 +98,7 @@ }, { "cell_type": "code", - "execution_count": 43, + "execution_count": 46, "id": "b4154ae6", "metadata": {}, "outputs": [], @@ -115,17 +115,18 @@ " entry = input(f\"Enter the price of {product}: \")\n", " price = float(entry)\n", " \n", - " # Paso 2: Validar lógica de negocio (Detecta valores negativos)\n", " if price < 0:\n", " print(f\"Error: Negative value ({price}) is not allowed.\")\n", - " continue \n", - "\n", - " total_price += price\n", - " valid_input = True\n", + " except ValueError:\n", + " \n", " \n", + " total_price += price\n", + " valid_input = True\n", + " \n", + " \n", " except ValueError:\n", "\n", - " print(f\"Error: '{entry}' is not a valid number. Please try again.\")\n", + " print(f\"Error: '{entry}' is not a valid number. Please try again.\")\n", " \n", " return total_price" ] From 7795b29c25757a55771ec67206193e8c598afe0e Mon Sep 17 00:00:00 2001 From: nailaikhenazenni-afk Date: Sat, 18 Apr 2026 01:29:17 +0200 Subject: [PATCH 07/20] Update lab-python-error-handling.ipynb --- lab-python-error-handling.ipynb | 39 ++++++++++++--------------------- 1 file changed, 14 insertions(+), 25 deletions(-) diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index 41a281a..370e624 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -98,42 +98,32 @@ }, { "cell_type": "code", - "execution_count": 46, + "execution_count": 48, "id": "b4154ae6", "metadata": {}, "outputs": [], "source": [ - "def calculate_total_price(customer_orders):\n", - " \n", - " total_price = 0.0\n", - "\n", - " for product in customer_orders:\n", + "def calculate_total_price(customer_orders): \n", + " total_price = 0.0\n", + " for product in customer_orders:\n", " valid_input = False\n", - " \n", " while not valid_input:\n", " try:\n", " entry = input(f\"Enter the price of {product}: \")\n", " price = float(entry)\n", - " \n", " if price < 0:\n", - " print(f\"Error: Negative value ({price}) is not allowed.\")\n", + " raise ValueError(\"Price cannot be negative. Please enter a valid price.\")\n", + " total_price += price\n", + " valid_input = True\n", " except ValueError:\n", - " \n", - " \n", - " total_price += price\n", - " valid_input = True\n", - " \n", - " \n", - " except ValueError:\n", - "\n", - " print(f\"Error: '{entry}' is not a valid number. Please try again.\")\n", + " print(f\"Error: '{entry}' is not a valid number. Please try again.\")\n", " \n", - " return total_price" + " return total_price" ] }, { "cell_type": "code", - "execution_count": 41, + "execution_count": 49, "id": "c568692b", "metadata": {}, "outputs": [], @@ -151,16 +141,15 @@ "\n", " customer_orders = set()\n", "\n", - " for orders in range(orders_count):\n", + " for order_index in range(orders_count):\n", " while True:\n", " try:\n", - " product_name = input(f\"Gather the name for product {orders}: \")\n", - " \n", - " if product_name not in inventory:\n", - " raise ValueError(f\"'{product_name}' does not exist in our catalog.\")\n", + " product_name = input(f\"Gather the name for product {order_index + 1}: \")\n", " \n", " if inventory[product_name] <= 0:\n", " raise ValueError(f\"'{product_name}' is currently out of stock.\")\n", + " if product_name not in inventory:\n", + " raise ValueError(f\"'{product_name}' does not exist in our catalog.\")\n", " \n", " customer_orders.add(product_name)\n", " break\n", From ff3bb48b6343148f62a725d3bfbba07810cde3b6 Mon Sep 17 00:00:00 2001 From: nailaikhenazenni-afk Date: Sat, 18 Apr 2026 02:01:24 +0200 Subject: [PATCH 08/20] Update lab-python-error-handling.ipynb --- lab-python-error-handling.ipynb | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index 370e624..37dd578 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -117,13 +117,12 @@ " valid_input = True\n", " except ValueError:\n", " print(f\"Error: '{entry}' is not a valid number. Please try again.\")\n", - " \n", - " return total_price" + " return total_price" ] }, { "cell_type": "code", - "execution_count": 49, + "execution_count": 51, "id": "c568692b", "metadata": {}, "outputs": [], @@ -145,17 +144,16 @@ " while True:\n", " try:\n", " product_name = input(f\"Gather the name for product {order_index + 1}: \")\n", - " \n", - " if inventory[product_name] <= 0:\n", - " raise ValueError(f\"'{product_name}' is currently out of stock.\")\n", " if product_name not in inventory:\n", " raise ValueError(f\"'{product_name}' does not exist in our catalog.\")\n", - " \n", + " print(f\"Invalid input: {e} Please try again.\")\n", " customer_orders.add(product_name)\n", + "\n", + " if inventory[product_name] <= 0:\n", + " raise ValueError(f\"'{product_name}' is currently out of stock.\")\n", " break\n", - " \n", " except ValueError as e:\n", - " print(f\"Invalid input: {e} Please try again.\")\n", + " print(f\"Invalid input: {e} Please try again.\")\n", "\n", " return customer_orders\n", " \n", From 729cfe3523fb565ebbae3bd415676ec3fcbaa830 Mon Sep 17 00:00:00 2001 From: nailaikhenazenni-afk Date: Sat, 18 Apr 2026 02:49:58 +0200 Subject: [PATCH 09/20] Update lab-python-error-handling.ipynb --- lab-python-error-handling.ipynb | 55 ++++++++++++++++++++------------- 1 file changed, 33 insertions(+), 22 deletions(-) diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index 37dd578..757f588 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -98,7 +98,7 @@ }, { "cell_type": "code", - "execution_count": 48, + "execution_count": 53, "id": "b4154ae6", "metadata": {}, "outputs": [], @@ -113,6 +113,7 @@ " price = float(entry)\n", " if price < 0:\n", " raise ValueError(\"Price cannot be negative. Please enter a valid price.\")\n", + " print(f'Error: {price} cannot be negative. Please enter a valid price.')\n", " total_price += price\n", " valid_input = True\n", " except ValueError:\n", @@ -122,40 +123,50 @@ }, { "cell_type": "code", - "execution_count": 51, + "execution_count": 52, "id": "c568692b", "metadata": {}, "outputs": [], "source": [ - "def get_customer_orders( inventory):\n", - " \n", - " while True:\n", - " try:\n", - " orders_count = int(input(\"Enter the number of customer orders: \"))\n", - " if orders_count < 0:\n", - " raise ValueError('Invalid number of orders! Please enter a non-negative integer.')\n", - " break\n", - " except ValueError as e:\n", - " print(f\"Error: {e}\")\n", - "\n", - " customer_orders = set()\n", - "\n", - " for order_index in range(orders_count):\n", + "def get_customer_orders(inventory: dict[str, int]) -> dict[str, int]:\n", + " # 1. Solicitar la cantidad total de pedidos\n", + " while True:\n", + " try:\n", + " orders_count = int(input(\"Enter the number of customer orders: \"))\n", + " if orders_count < 0:\n", + " raise ValueError(\"The number of orders cannot be negative.\")\n", + " break\n", + " except ValueError as e:\n", + " print(f\"Error: {e} Please enter a valid integer.\")\n", + "\n", + " customer_orders = {}\n", + "\n", + " # 2. Recopilar cada producto\n", + " for i in range(orders_count):\n", " while True:\n", " try:\n", - " product_name = input(f\"Gather the name for product {order_index + 1}: \")\n", + " product_name = input(f\"Gather the name for product {i + 1}: \")\n", + "\n", + " # Verificación 1: ¿Existe en el catálogo?\n", " if product_name not in inventory:\n", " raise ValueError(f\"'{product_name}' does not exist in our catalog.\")\n", - " print(f\"Invalid input: {e} Please try again.\")\n", - " customer_orders.add(product_name)\n", + " \n", + " # Verificación 2: ¿Hay stock disponible?\n", + " # Consideramos cuántos ya ha pedido el usuario en este turno\n", + " already_ordered = customer_orders.get(product_name, 0)\n", + " if inventory[product_name] <= already_ordered:\n", + " raise ValueError(f\"'{product_name}' is currently out of stock or insufficient units.\")\n", "\n", - " if inventory[product_name] <= 0:\n", - " raise ValueError(f\"'{product_name}' is currently out of stock.\")\n", + " # Si pasa las validaciones, lo añadimos al \"carrito\" (diccionario)\n", + " customer_orders[product_name] = already_ordered + 1\n", + " print(f\"Added {product_name} to your order.\")\n", " break\n", + " \n", " except ValueError as e:\n", - " print(f\"Invalid input: {e} Please try again.\")\n", + " print(f\"Invalid input: {e} Please try again.\")\n", "\n", " return customer_orders\n", + "\n", " \n", "\n", " " From f71143d7e3ab87cb6dafda7fa1bb3f1e99e4cbaa Mon Sep 17 00:00:00 2001 From: nailaikhenazenni-afk Date: Sat, 18 Apr 2026 03:11:38 +0200 Subject: [PATCH 10/20] Update lab-python-error-handling.ipynb --- lab-python-error-handling.ipynb | 19 +++---------------- 1 file changed, 3 insertions(+), 16 deletions(-) diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index 757f588..c76aad7 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -123,13 +123,12 @@ }, { "cell_type": "code", - "execution_count": 52, + "execution_count": null, "id": "c568692b", "metadata": {}, "outputs": [], "source": [ - "def get_customer_orders(inventory: dict[str, int]) -> dict[str, int]:\n", - " # 1. Solicitar la cantidad total de pedidos\n", + "def get_customer_orders(inventory):\n", " while True:\n", " try:\n", " orders_count = int(input(\"Enter the number of customer orders: \"))\n", @@ -140,30 +139,18 @@ " print(f\"Error: {e} Please enter a valid integer.\")\n", "\n", " customer_orders = {}\n", - "\n", - " # 2. Recopilar cada producto\n", " for i in range(orders_count):\n", " while True:\n", " try:\n", " product_name = input(f\"Gather the name for product {i + 1}: \")\n", - "\n", - " # Verificación 1: ¿Existe en el catálogo?\n", " if product_name not in inventory:\n", " raise ValueError(f\"'{product_name}' does not exist in our catalog.\")\n", - " \n", - " # Verificación 2: ¿Hay stock disponible?\n", - " # Consideramos cuántos ya ha pedido el usuario en este turno\n", " already_ordered = customer_orders.get(product_name, 0)\n", " if inventory[product_name] <= already_ordered:\n", " raise ValueError(f\"'{product_name}' is currently out of stock or insufficient units.\")\n", - "\n", - " # Si pasa las validaciones, lo añadimos al \"carrito\" (diccionario)\n", - " customer_orders[product_name] = already_ordered + 1\n", - " print(f\"Added {product_name} to your order.\")\n", " break\n", - " \n", " except ValueError as e:\n", - " print(f\"Invalid input: {e} Please try again.\")\n", + " print(f\"Error: {e} Please try again.\")\n", "\n", " return customer_orders\n", "\n", From 53f9aaefd52d282dbf52b45eca695b886520859a Mon Sep 17 00:00:00 2001 From: nailaikhenazenni-afk Date: Sat, 18 Apr 2026 03:14:36 +0200 Subject: [PATCH 11/20] Update lab-python-error-handling.ipynb --- lab-python-error-handling.ipynb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index c76aad7..ca1a598 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -98,7 +98,7 @@ }, { "cell_type": "code", - "execution_count": 53, + "execution_count": null, "id": "b4154ae6", "metadata": {}, "outputs": [], @@ -113,11 +113,11 @@ " price = float(entry)\n", " if price < 0:\n", " raise ValueError(\"Price cannot be negative. Please enter a valid price.\")\n", - " print(f'Error: {price} cannot be negative. Please enter a valid price.')\n", " total_price += price\n", " valid_input = True\n", " except ValueError:\n", " print(f\"Error: '{entry}' is not a valid number. Please try again.\")\n", + " print (f\"Error: '{price}' cannot be negative. Please enter a valid price.\") \n", " return total_price" ] }, From 97505a41a452833a2ebc51cb7fe14f6003694159 Mon Sep 17 00:00:00 2001 From: nailaikhenazenni-afk Date: Sun, 10 May 2026 16:13:58 +0200 Subject: [PATCH 12/20] Update lab-python-error-handling.ipynb --- lab-python-error-handling.ipynb | 81 ++++++++++++++++++++++++--------- 1 file changed, 59 insertions(+), 22 deletions(-) diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index ca1a598..bce80f4 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -75,11 +75,12 @@ }, { "cell_type": "code", - "execution_count": 39, + "execution_count": 10, "id": "464b1353", "metadata": {}, "outputs": [], "source": [ + "# Step 1:\n", "def initialize_inventory(products):\n", " inventory = {}\n", " for product in products:\n", @@ -98,62 +99,80 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 11, "id": "b4154ae6", "metadata": {}, "outputs": [], "source": [ - "def calculate_total_price(customer_orders): \n", - " total_price = 0.0\n", - " for product in customer_orders:\n", - " valid_input = False\n", - " while not valid_input:\n", + "def calculate_total_price(customer_orders):\n", + " total_price = 0.0\n", + " \n", + " for product in customer_orders:\n", + " while True:\n", " try:\n", " entry = input(f\"Enter the price of {product}: \")\n", " price = float(entry)\n", + " \n", " if price < 0:\n", - " raise ValueError(\"Price cannot be negative. Please enter a valid price.\")\n", + " print(\"Error: Price cannot be negative. Please try again.\")\n", + " continue # Reinicia el bucle para pedir el precio otra vez\n", + " \n", " total_price += price\n", - " valid_input = True\n", + " break # Sale del bucle while si todo es correcto\n", + " \n", " except ValueError:\n", - " print(f\"Error: '{entry}' is not a valid number. Please try again.\")\n", - " print (f\"Error: '{price}' cannot be negative. Please enter a valid price.\") \n", - " return total_price" + " print(f\"Error: '{entry}' is not a valid numeric value. Please try again.\")\n", + " \n", + " return total_price" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 12, "id": "c568692b", "metadata": {}, "outputs": [], "source": [ + "# Step 3:\n", "def get_customer_orders(inventory):\n", + " # 1. Validar la cantidad de órdenes totales\n", " while True:\n", " try:\n", - " orders_count = int(input(\"Enter the number of customer orders: \"))\n", + " entry = input(\"Enter the number of customer orders: \")\n", + " orders_count = int(entry)\n", " if orders_count < 0:\n", - " raise ValueError(\"The number of orders cannot be negative.\")\n", + " print(\"Error: The number of orders cannot be negative.\")\n", + " continue\n", " break\n", - " except ValueError as e:\n", - " print(f\"Error: {e} Please enter a valid integer.\")\n", + " except ValueError:\n", + " print(f\"Error: '{entry}' is not a valid number. Please enter an integer.\")\n", "\n", " customer_orders = {}\n", + "\n", + " # 2. Validar cada nombre de producto y su stock\n", " for i in range(orders_count):\n", " while True:\n", + " product_name = input(f\"Enter the name for product {i + 1}: \").lower()\n", + " \n", " try:\n", - " product_name = input(f\"Gather the name for product {i + 1}: \")\n", + " # Comprobar existencia\n", " if product_name not in inventory:\n", " raise ValueError(f\"'{product_name}' does not exist in our catalog.\")\n", + " \n", + " # Comprobar stock disponible\n", + " # (Consideramos cuántos ya hemos pedido en este mismo ciclo)\n", " already_ordered = customer_orders.get(product_name, 0)\n", " if inventory[product_name] <= already_ordered:\n", - " raise ValueError(f\"'{product_name}' is currently out of stock or insufficient units.\")\n", - " break\n", + " raise ValueError(f\"'{product_name}' is out of stock or insufficient units.\")\n", + "\n", + " # AGREGAR AL DICCIONARIO: Esta es la parte que faltaba\n", + " customer_orders[product_name] = already_ordered + 1\n", + " break \n", + "\n", " except ValueError as e:\n", " print(f\"Error: {e} Please try again.\")\n", "\n", " return customer_orders\n", - "\n", " \n", "\n", " " @@ -161,10 +180,28 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 13, "id": "fa19394e", "metadata": {}, "outputs": [], + "source": [ + "# step 4:\n", + "def manage_customer_orders():\n", + "\n", + " products_list = ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n", + " inventory = initialize_inventory(products_list)\n", + " total_price= calculate_total_price(inventory)\n", + " customer_orders = get_customer_orders(inventory)\n", + " \n", + " return\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b3d0311b", + "metadata": {}, + "outputs": [], "source": [] } ], From 56fb7d0934eef9352ee5178640ec8866093bffad Mon Sep 17 00:00:00 2001 From: nailaikhenazenni-afk Date: Sun, 10 May 2026 16:34:54 +0200 Subject: [PATCH 13/20] Update lab-python-error-handling.ipynb --- lab-python-error-handling.ipynb | 51 +++++++++++++++++++++++---------- 1 file changed, 36 insertions(+), 15 deletions(-) diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index bce80f4..bd85d7d 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -128,14 +128,14 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 15, "id": "c568692b", "metadata": {}, "outputs": [], "source": [ "# Step 3:\n", - "def get_customer_orders(inventory):\n", - " # 1. Validar la cantidad de órdenes totales\n", + "def get_customer_orders(inventory: dict) -> dict:\n", + " # 1. Validar la cantidad total de pedidos\n", " while True:\n", " try:\n", " entry = input(\"Enter the number of customer orders: \")\n", @@ -149,23 +149,22 @@ "\n", " customer_orders = {}\n", "\n", - " # 2. Validar cada nombre de producto y su stock\n", + " # 2. Validar cada producto y su disponibilidad individual\n", " for i in range(orders_count):\n", " while True:\n", - " product_name = input(f\"Enter the name for product {i + 1}: \").lower()\n", + " product_name = input(f\"Enter the name for product {i + 1}: \").lower().strip()\n", " \n", " try:\n", - " # Comprobar existencia\n", + " # Comprobar si el producto existe en el inventario\n", " if product_name not in inventory:\n", " raise ValueError(f\"'{product_name}' does not exist in our catalog.\")\n", " \n", - " # Comprobar stock disponible\n", - " # (Consideramos cuántos ya hemos pedido en este mismo ciclo)\n", + " # Comprobar si hay suficiente stock disponible\n", " already_ordered = customer_orders.get(product_name, 0)\n", " if inventory[product_name] <= already_ordered:\n", " raise ValueError(f\"'{product_name}' is out of stock or insufficient units.\")\n", "\n", - " # AGREGAR AL DICCIONARIO: Esta es la parte que faltaba\n", + " # Registrar o incrementar el pedido en el diccionario\n", " customer_orders[product_name] = already_ordered + 1\n", " break \n", "\n", @@ -180,20 +179,42 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": null, "id": "fa19394e", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: Invalid quantity! Please enter a non-negative value.\n" + ] + } + ], "source": [ "# step 4:\n", "def manage_customer_orders():\n", - "\n", " products_list = ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n", + " \n", + " # 1. Creamos el inventario\n", " inventory = initialize_inventory(products_list)\n", - " total_price= calculate_total_price(inventory)\n", + " \n", + " # 2. Obtenemos los pedidos (ESTO debe ir antes del precio)\n", " customer_orders = get_customer_orders(inventory)\n", - " \n", - " return\n" + " \n", + " # 3. Calculamos el precio basándonos en los pedidos recibidos\n", + " total_price = calculate_total_price(customer_orders)\n", + " \n", + " # 4. MOSTRAMOS los resultados en la terminal\n", + " print(f\"\\n--- Final Summary ---\")\n", + " print(f\"Products ordered: {customer_orders}\")\n", + " print(f\"Total price: ${total_price:.2f}\")\n", + " \n", + " return total_price\n", + "\n", + "# --- EL PASO FINAL: Ejecutar la función ---\n", + "# Sin esta línea, no aparecerá nada en tu pantalla.\n", + "manage_customer_orders()" ] }, { From 94ca1e574846819e68120d7f96209a4fde4d6374 Mon Sep 17 00:00:00 2001 From: nailaikhenazenni-afk Date: Sun, 10 May 2026 16:52:12 +0200 Subject: [PATCH 14/20] Update lab-python-error-handling.ipynb --- lab-python-error-handling.ipynb | 44 ++++++++++++++++----------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index bd85d7d..1088601 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -75,7 +75,7 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 3, "id": "464b1353", "metadata": {}, "outputs": [], @@ -99,7 +99,7 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 4, "id": "b4154ae6", "metadata": {}, "outputs": [], @@ -109,26 +109,24 @@ " \n", " for product in customer_orders:\n", " while True:\n", + " entry = input(f\"Enter the price of {product}: \")\n", " try:\n", - " entry = input(f\"Enter the price of {product}: \")\n", " price = float(entry)\n", - " \n", " if price < 0:\n", - " print(\"Error: Price cannot be negative. Please try again.\")\n", - " continue # Reinicia el bucle para pedir el precio otra vez\n", - " \n", - " total_price += price\n", - " break # Sale del bucle while si todo es correcto\n", - " \n", + " print(\"Error: Price cannot be negative.\")\n", + " else:\n", + " # Si el precio es válido, lo sumamos y salimos del bucle\n", + " total_price += price\n", + " break\n", " except ValueError:\n", - " print(f\"Error: '{entry}' is not a valid numeric value. Please try again.\")\n", + " print(f\"Error: '{entry}' is not a valid number. Please try again.\")\n", " \n", " return total_price" ] }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 5, "id": "c568692b", "metadata": {}, "outputs": [], @@ -149,32 +147,33 @@ "\n", " customer_orders = {}\n", "\n", - " # 2. Validar cada producto y su disponibilidad individual\n", + " # 2. Validar cada producto y su disponibilidad\n", " for i in range(orders_count):\n", " while True:\n", " product_name = input(f\"Enter the name for product {i + 1}: \").lower().strip()\n", " \n", " try:\n", - " # Comprobar si el producto existe en el inventario\n", + " # Consistencia: ¿Existe el producto?\n", " if product_name not in inventory:\n", " raise ValueError(f\"'{product_name}' does not exist in our catalog.\")\n", " \n", - " # Comprobar si hay suficiente stock disponible\n", + " # Consistencia: ¿Hay stock físico inicial?\n", + " if inventory[product_name] <= 0:\n", + " raise ValueError(f\"'{product_name}' is completely out of stock.\")\n", + " \n", + " # Consistencia: ¿Queda stock considerando lo que ya pedí en esta sesión?\n", " already_ordered = customer_orders.get(product_name, 0)\n", - " if inventory[product_name] <= already_ordered:\n", - " raise ValueError(f\"'{product_name}' is out of stock or insufficient units.\")\n", + " if already_ordered >= inventory[product_name]:\n", + " raise ValueError(f\"No more units of '{product_name}' available.\")\n", "\n", - " # Registrar o incrementar el pedido en el diccionario\n", + " # Si pasa las validaciones, se registra\n", " customer_orders[product_name] = already_ordered + 1\n", " break \n", "\n", " except ValueError as e:\n", " print(f\"Error: {e} Please try again.\")\n", "\n", - " return customer_orders\n", - " \n", - "\n", - " " + " return customer_orders" ] }, { @@ -187,6 +186,7 @@ "name": "stdout", "output_type": "stream", "text": [ + "Error: invalid literal for int() with base 10: ''\n", "Error: Invalid quantity! Please enter a non-negative value.\n" ] } From c0b5af786cc01eee111af8f0c95ce1ee5063e615 Mon Sep 17 00:00:00 2001 From: nailaikhenazenni-afk Date: Sun, 10 May 2026 17:03:36 +0200 Subject: [PATCH 15/20] Update lab-python-error-handling.ipynb --- lab-python-error-handling.ipynb | 25 +++++-------------------- 1 file changed, 5 insertions(+), 20 deletions(-) diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index 1088601..a0c5667 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -75,7 +75,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 2, "id": "464b1353", "metadata": {}, "outputs": [], @@ -99,7 +99,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 5, "id": "b4154ae6", "metadata": {}, "outputs": [], @@ -126,29 +126,15 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 4, "id": "c568692b", "metadata": {}, "outputs": [], "source": [ "# Step 3:\n", - "def get_customer_orders(inventory: dict) -> dict:\n", - " # 1. Validar la cantidad total de pedidos\n", - " while True:\n", - " try:\n", - " entry = input(\"Enter the number of customer orders: \")\n", - " orders_count = int(entry)\n", - " if orders_count < 0:\n", - " print(\"Error: The number of orders cannot be negative.\")\n", - " continue\n", - " break\n", - " except ValueError:\n", - " print(f\"Error: '{entry}' is not a valid number. Please enter an integer.\")\n", - "\n", + "def get_customer_orders(inventory):\n", " customer_orders = {}\n", - "\n", - " # 2. Validar cada producto y su disponibilidad\n", - " for i in range(orders_count):\n", + " for i in range(3): # Assuming a maximum of 5 products can be ordered\n", " while True:\n", " product_name = input(f\"Enter the name for product {i + 1}: \").lower().strip()\n", " \n", @@ -186,7 +172,6 @@ "name": "stdout", "output_type": "stream", "text": [ - "Error: invalid literal for int() with base 10: ''\n", "Error: Invalid quantity! Please enter a non-negative value.\n" ] } From 5969a2ff636c9ce75008c1e5a0cf3127751c3bcb Mon Sep 17 00:00:00 2001 From: nailaikhenazenni-afk Date: Sun, 10 May 2026 17:14:25 +0200 Subject: [PATCH 16/20] Update lab-python-error-handling.ipynb --- lab-python-error-handling.ipynb | 42 ++++++++++++++++++++------------- 1 file changed, 25 insertions(+), 17 deletions(-) diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index a0c5667..e848c2c 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -99,11 +99,12 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": null, "id": "b4154ae6", "metadata": {}, "outputs": [], "source": [ + "# Step 2:\n", "def calculate_total_price(customer_orders):\n", " total_price = 0.0\n", " \n", @@ -115,7 +116,6 @@ " if price < 0:\n", " print(\"Error: Price cannot be negative.\")\n", " else:\n", - " # Si el precio es válido, lo sumamos y salimos del bucle\n", " total_price += price\n", " break\n", " except ValueError:\n", @@ -126,33 +126,44 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": null, "id": "c568692b", "metadata": {}, "outputs": [], "source": [ - "# Step 3:\n", + "# Step 31:\n", "def get_customer_orders(inventory):\n", + " # 1. Pedir y validar el número total de pedidos\n", + " while True:\n", + " try:\n", + " num_orders_input = input(\"How many products do you want to order? \")\n", + " num_orders = int(num_orders_input)\n", + " if num_orders < 0:\n", + " print(\"Error: The number of orders cannot be negative.\")\n", + " continue\n", + " break\n", + " except ValueError:\n", + " print(f\"Error: '{num_orders_input}' is not a valid number. Please enter an integer.\")\n", + "\n", " customer_orders = {}\n", - " for i in range(3): # Assuming a maximum of 5 products can be ordered\n", + "\n", + " # 2. Bucle basado en la cantidad de pedidos elegida por el usuario\n", + " for i in range(num_orders):\n", " while True:\n", " product_name = input(f\"Enter the name for product {i + 1}: \").lower().strip()\n", " \n", " try:\n", - " # Consistencia: ¿Existe el producto?\n", + " # Validación de existencia\n", " if product_name not in inventory:\n", " raise ValueError(f\"'{product_name}' does not exist in our catalog.\")\n", " \n", - " # Consistencia: ¿Hay stock físico inicial?\n", - " if inventory[product_name] <= 0:\n", - " raise ValueError(f\"'{product_name}' is completely out of stock.\")\n", - " \n", - " # Consistencia: ¿Queda stock considerando lo que ya pedí en esta sesión?\n", + " # Validación de disponibilidad inmediata\n", + " # Comparamos lo ya pedido contra lo que hay en el inventario\n", " already_ordered = customer_orders.get(product_name, 0)\n", - " if already_ordered >= inventory[product_name]:\n", - " raise ValueError(f\"No more units of '{product_name}' available.\")\n", + " if inventory[product_name] <= already_ordered:\n", + " raise ValueError(f\"'{product_name}' is out of stock or no more units available.\")\n", "\n", - " # Si pasa las validaciones, se registra\n", + " # Si todo está bien, lo registramos\n", " customer_orders[product_name] = already_ordered + 1\n", " break \n", "\n", @@ -196,9 +207,6 @@ " print(f\"Total price: ${total_price:.2f}\")\n", " \n", " return total_price\n", - "\n", - "# --- EL PASO FINAL: Ejecutar la función ---\n", - "# Sin esta línea, no aparecerá nada en tu pantalla.\n", "manage_customer_orders()" ] }, From 06723bff33c29d8a475b9a6181c6ac3f2e763a7e Mon Sep 17 00:00:00 2001 From: nailaikhenazenni-afk Date: Sun, 10 May 2026 18:09:38 +0200 Subject: [PATCH 17/20] Update lab-python-error-handling.ipynb --- lab-python-error-handling.ipynb | 96 +++++++++++++++++---------------- 1 file changed, 51 insertions(+), 45 deletions(-) diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index e848c2c..b20908c 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -94,12 +94,13 @@ " except ValueError as error:\n", " print(f\"Error: {error}\")\n", " inventory[product] = quantity\n", - " return inventory" + " return inventory\n", + " " ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "id": "b4154ae6", "metadata": {}, "outputs": [], @@ -114,7 +115,7 @@ " try:\n", " price = float(entry)\n", " if price < 0:\n", - " print(\"Error: Price cannot be negative.\")\n", + " print(\"Error: Price cannot be negative. Please try again.\")\n", " else:\n", " total_price += price\n", " break\n", @@ -126,97 +127,102 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "id": "c568692b", "metadata": {}, "outputs": [], "source": [ - "# Step 31:\n", + "# Step 3:\n", "def get_customer_orders(inventory):\n", - " # 1. Pedir y validar el número total de pedidos\n", + " # 1. Validar el número de pedidos (num_orders)\n", " while True:\n", " try:\n", - " num_orders_input = input(\"How many products do you want to order? \")\n", - " num_orders = int(num_orders_input)\n", + " entry = input(\"How many products do you want to order? \")\n", + " num_orders = int(entry)\n", " if num_orders < 0:\n", " print(\"Error: The number of orders cannot be negative.\")\n", " continue\n", " break\n", " except ValueError:\n", - " print(f\"Error: '{num_orders_input}' is not a valid number. Please enter an integer.\")\n", + " print(f\"Error: '{entry}' is not a valid number. Please enter an integer.\")\n", "\n", " customer_orders = {}\n", "\n", - " # 2. Bucle basado en la cantidad de pedidos elegida por el usuario\n", + " # 2. Bucle de pedidos con validaciones específicas\n", " for i in range(num_orders):\n", " while True:\n", + " # Normalizamos la entrada para coincidir con las llaves de inventory\n", " product_name = input(f\"Enter the name for product {i + 1}: \").lower().strip()\n", " \n", " try:\n", - " # Validación de existencia\n", + " # Caso A: El producto no existe en el catálogo\n", " if product_name not in inventory:\n", - " raise ValueError(f\"'{product_name}' does not exist in our catalog.\")\n", + " raise ValueError(f\"Product '{product_name}' was not found in our catalog.\")\n", " \n", - " # Validación de disponibilidad inmediata\n", - " # Comparamos lo ya pedido contra lo que hay en el inventario\n", - " already_ordered = customer_orders.get(product_name, 0)\n", - " if inventory[product_name] <= already_ordered:\n", - " raise ValueError(f\"'{product_name}' is out of stock or no more units available.\")\n", - "\n", - " # Si todo está bien, lo registramos\n", - " customer_orders[product_name] = already_ordered + 1\n", + " # Caso B: El producto existe pero no tiene stock inicial\n", + " if inventory[product_name] == 0:\n", + " raise ValueError(f\"Sorry, '{product_name}' is currently out of stock.\")\n", + " \n", + " # Caso C: Validar contra lo ya pedido en esta sesión\n", + " current_count = customer_orders.get(product_name, 0)\n", + " if current_count >= inventory[product_name]:\n", + " raise ValueError(f\"Cannot add more '{product_name}'. Only {inventory[product_name]} available.\")\n", + "\n", + " # Si pasa todas las aduanas, lo añadimos\n", + " customer_orders[product_name] = current_count + 1\n", + " print(f\"-> '{product_name}' added to your order.\")\n", " break \n", "\n", " except ValueError as e:\n", - " print(f\"Error: {e} Please try again.\")\n", + " # Imprimimos el mensaje específico que lanzamos arriba\n", + " print(f\"Invalid Entry: {e}\")\n", "\n", - " return customer_orders" + " return customer_orders\n", + "\n", + " " ] }, { "cell_type": "code", "execution_count": null, - "id": "fa19394e", + "id": "5375f1f8", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "Error: Invalid quantity! Please enter a non-negative value.\n" + "Error: invalid literal for int() with base 10: ''\n" ] } ], "source": [ - "# step 4:\n", "def manage_customer_orders():\n", " products_list = ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n", " \n", - " # 1. Creamos el inventario\n", + " # 1. Inicializar el inventario (se asume que esta función existe)\n", " inventory = initialize_inventory(products_list)\n", " \n", - " # 2. Obtenemos los pedidos (ESTO debe ir antes del precio)\n", + " # 2. Obtener los pedidos del cliente (usando la función que validamos antes)\n", " customer_orders = get_customer_orders(inventory)\n", - " \n", - " # 3. Calculamos el precio basándonos en los pedidos recibidos\n", - " total_price = calculate_total_price(customer_orders)\n", - " \n", - " # 4. MOSTRAMOS los resultados en la terminal\n", - " print(f\"\\n--- Final Summary ---\")\n", - " print(f\"Products ordered: {customer_orders}\")\n", - " print(f\"Total price: ${total_price:.2f}\")\n", - " \n", - " return total_price\n", + "\n", + " # 3. Mostrar el resumen de lo pedido\n", + " print(f\"\\nSummary of your order: {customer_orders}\")\n", + "\n", + " # 4. Actualizar el inventario real restando lo pedido\n", + " for product, quantity in customer_orders.items():\n", + " inventory[product] -= quantity\n", + "\n", + " # 5. Mostrar el inventario final actualizado\n", + " print(\"\\nFinal Inventory after orders:\")\n", + " for product, stock in inventory.items():\n", + " print(f\"{product}: {stock}\")\n", + "\n", + " return customer_orders\n", + "\n", + "# --- ÚNICA LÍNEA PARA ARRANCAR TODO EL PROGRAMA ---\n", "manage_customer_orders()" ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "b3d0311b", - "metadata": {}, - "outputs": [], - "source": [] } ], "metadata": { From a93cc348089f7513cfc81fb39a42c8a288b6529a Mon Sep 17 00:00:00 2001 From: nailaikhenazenni-afk Date: Sun, 10 May 2026 18:19:15 +0200 Subject: [PATCH 18/20] Update lab-python-error-handling.ipynb --- lab-python-error-handling.ipynb | 57 +++++++++++++++------------------ 1 file changed, 26 insertions(+), 31 deletions(-) diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index b20908c..b3a5d0a 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -75,7 +75,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 3, "id": "464b1353", "metadata": {}, "outputs": [], @@ -100,7 +100,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 4, "id": "b4154ae6", "metadata": {}, "outputs": [], @@ -134,7 +134,7 @@ "source": [ "# Step 3:\n", "def get_customer_orders(inventory):\n", - " # 1. Validar el número de pedidos (num_orders)\n", + " # 1. Validate the number of orders\n", " while True:\n", " try:\n", " entry = input(\"How many products do you want to order? \")\n", @@ -145,39 +145,34 @@ " break\n", " except ValueError:\n", " print(f\"Error: '{entry}' is not a valid number. Please enter an integer.\")\n", - "\n", - " customer_orders = {}\n", - "\n", - " # 2. Bucle de pedidos con validaciones específicas\n", + " \n", + " # 2. Collect valid product names\n", + " customer_orders = set()\n", + " \n", " for i in range(num_orders):\n", " while True:\n", - " # Normalizamos la entrada para coincidir con las llaves de inventory\n", - " product_name = input(f\"Enter the name for product {i + 1}: \").lower().strip()\n", + " product_name = input(f\"Enter product {i + 1}: \").strip().lower()\n", " \n", - " try:\n", - " # Caso A: El producto no existe en el catálogo\n", - " if product_name not in inventory:\n", - " raise ValueError(f\"Product '{product_name}' was not found in our catalog.\")\n", - " \n", - " # Caso B: El producto existe pero no tiene stock inicial\n", - " if inventory[product_name] == 0:\n", - " raise ValueError(f\"Sorry, '{product_name}' is currently out of stock.\")\n", - " \n", - " # Caso C: Validar contra lo ya pedido en esta sesión\n", - " current_count = customer_orders.get(product_name, 0)\n", - " if current_count >= inventory[product_name]:\n", - " raise ValueError(f\"Cannot add more '{product_name}'. Only {inventory[product_name]} available.\")\n", - "\n", - " # Si pasa todas las aduanas, lo añadimos\n", - " customer_orders[product_name] = current_count + 1\n", - " print(f\"-> '{product_name}' added to your order.\")\n", - " break \n", + " # Check if product exists in inventory\n", + " if product_name not in inventory:\n", + " print(f\"Error: '{product_name}' is not in our inventory.\")\n", + " print(f\"Available products: {', '.join(inventory.keys())}\")\n", + " continue\n", + " \n", + " # Check if product has stock available\n", + " if inventory[product_name] <= 0:\n", + " print(f\"Error: '{product_name}' is out of stock.\")\n", + " continue\n", + " \n", + " # Valid product with stock available\n", + " customer_orders.add(product_name)\n", + " break\n", + " \n", + " return customer_orders\n", "\n", - " except ValueError as e:\n", - " # Imprimimos el mensaje específico que lanzamos arriba\n", - " print(f\"Invalid Entry: {e}\")\n", "\n", - " return customer_orders\n", + " \n", + " \n", "\n", " " ] From 003c2b9af16e810ad2a78c7faac8c91d97d99193 Mon Sep 17 00:00:00 2001 From: nailaikhenazenni-afk Date: Sun, 10 May 2026 18:35:44 +0200 Subject: [PATCH 19/20] Update lab-python-error-handling.ipynb --- lab-python-error-handling.ipynb | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index b3a5d0a..e882f65 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -75,7 +75,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 1, "id": "464b1353", "metadata": {}, "outputs": [], @@ -100,7 +100,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 2, "id": "b4154ae6", "metadata": {}, "outputs": [], @@ -127,7 +127,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": null, "id": "c568692b", "metadata": {}, "outputs": [], @@ -194,30 +194,29 @@ "source": [ "def manage_customer_orders():\n", " products_list = ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n", - " \n", - " # 1. Inicializar el inventario (se asume que esta función existe)\n", + "\n", " inventory = initialize_inventory(products_list)\n", - " \n", - " # 2. Obtener los pedidos del cliente (usando la función que validamos antes)\n", " customer_orders = get_customer_orders(inventory)\n", "\n", - " # 3. Mostrar el resumen de lo pedido\n", - " print(f\"\\nSummary of your order: {customer_orders}\")\n", + " print(f\"\\nSummary of your order: {customer_orders}\") # ✅ fixed f-string\n", "\n", - " # 4. Actualizar el inventario real restando lo pedido\n", - " for product, quantity in customer_orders.items():\n", - " inventory[product] -= quantity\n", + " for product in customer_orders: # ✅ iterate set directly, no .items()\n", + " inventory[product] -= 1 # ✅ subtract 1 per product\n", "\n", - " # 5. Mostrar el inventario final actualizado\n", " print(\"\\nFinal Inventory after orders:\")\n", " for product, stock in inventory.items():\n", - " print(f\"{product}: {stock}\")\n", - "\n", + " print(f\" {product}: {stock}\")\n", " return customer_orders\n", - "\n", - "# --- ÚNICA LÍNEA PARA ARRANCAR TODO EL PROGRAMA ---\n", "manage_customer_orders()" ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "621c5fc9", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { From 37e4093f44bb2327d3d90e7095384841ce216d9c Mon Sep 17 00:00:00 2001 From: nailaikhenazenni-afk Date: Sun, 10 May 2026 18:45:56 +0200 Subject: [PATCH 20/20] Update lab-python-error-handling.ipynb --- lab-python-error-handling.ipynb | 71 +++++++++++++++++++++++++++++---- 1 file changed, 63 insertions(+), 8 deletions(-) diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index e882f65..276672e 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -75,7 +75,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 6, "id": "464b1353", "metadata": {}, "outputs": [], @@ -100,7 +100,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 7, "id": "b4154ae6", "metadata": {}, "outputs": [], @@ -127,7 +127,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "id": "c568692b", "metadata": {}, "outputs": [], @@ -151,7 +151,7 @@ " \n", " for i in range(num_orders):\n", " while True:\n", - " product_name = input(f\"Enter product {i + 1}: \").strip().lower()\n", + " product_name = input(f\"Enter product {i + 1}: \")\n", " \n", " # Check if product exists in inventory\n", " if product_name not in inventory:\n", @@ -179,7 +179,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 9, "id": "5375f1f8", "metadata": {}, "outputs": [ @@ -187,8 +187,26 @@ "name": "stdout", "output_type": "stream", "text": [ - "Error: invalid literal for int() with base 10: ''\n" + "\n", + "Summary of your order: {'hat', 'book', 'mug'}\n", + "\n", + "Final Inventory after orders:\n", + " t-shirt: 2\n", + " mug: 2\n", + " hat: 3\n", + " book: 4\n", + " keychain: 1\n" ] + }, + { + "data": { + "text/plain": [ + "{'book', 'hat', 'mug'}" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ @@ -215,8 +233,45 @@ "execution_count": null, "id": "621c5fc9", "metadata": {}, - "outputs": [], - "source": [] + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Error: Invalid quantity! Please enter a non-negative value.\n", + "Error: 'sock' is not in our inventory.\n", + "Available products: t-shirt, mug, hat, book, keychain\n", + "Error: 'ten' is not in our inventory.\n", + "Available products: t-shirt, mug, hat, book, keychain\n", + "Error: '10' is not in our inventory.\n", + "Available products: t-shirt, mug, hat, book, keychain\n", + "Error: '' is not in our inventory.\n", + "Available products: t-shirt, mug, hat, book, keychain\n", + "Error: '' is not in our inventory.\n", + "Available products: t-shirt, mug, hat, book, keychain\n", + "Error: '' is not in our inventory.\n", + "Available products: t-shirt, mug, hat, book, keychain\n" + ] + } + ], + "source": [ + "def manage_customer_orders():\n", + " products_list = ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n", + "\n", + " inventory = initialize_inventory(products_list)\n", + " customer_orders = get_customer_orders(inventory)\n", + "\n", + " print(f\"\\nSummary of your order: {customer_orders}\") # ✅ fixed f-string\n", + "\n", + " for product in customer_orders: # ✅ iterate set directly, no .items()\n", + " inventory[product] -= 1 # ✅ subtract 1 per product\n", + "\n", + " print(\"\\nFinal Inventory after orders:\")\n", + " for product, stock in inventory.items():\n", + " print(f\" {product}: {stock}\")\n", + " return customer_orders\n", + "manage_customer_orders()" + ] } ], "metadata": {