From 42818c9923b33aa0c8b23060b4b50c06dab3daa0 Mon Sep 17 00:00:00 2001 From: nailaikhenazenni-afk Date: Fri, 17 Apr 2026 22:04:49 +0200 Subject: [PATCH 01/11] 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/11] 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/11] 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/11] 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/11] 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/11] 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/11] 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/11] 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/11] 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/11] 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/11] 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" ] },