From 3f228688f6fa895befd9399dd92308dc2e1a80d3 Mon Sep 17 00:00:00 2001 From: Lautaro Date: Thu, 15 Jan 2026 14:32:56 +0100 Subject: [PATCH 1/4] Day 4.1 Error Handling --- lab-python-error-handling.ipynb | 44 +++++++++++++++++++++++++++++++-- 1 file changed, 42 insertions(+), 2 deletions(-) diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index f4c6ef6..9f9cd23 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -72,11 +72,51 @@ "\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": 7, + "id": "5a77a67b", + "metadata": {}, + "outputs": [], + "source": [ + "productos = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n", + "def inventario_inicial(productos):\n", + " inventario = {}\n", + " for producto in productos:\n", + " cantidad_valida = False\n", + " while not cantidad_valida: \n", + " try:\n", + " cantidades_producto = int(input(f\"Ingrese la cantidad inicial de {producto}: \"))\n", + " if cantidades_producto < 0:\n", + " print(\"Por favor, ingrese un número entero no negativo.\")\n", + " else:\n", + " cantidad_valida = True\n", + " return inventario\n", + " except ValueError:\n", + " print(\"Ha ocurrido un error. Por favor, ingrese un número entero.\")\n", + " finally:\n", + " inventario[producto] = cantidades_producto\n", + " print (f\"Inventario inicial de {producto}: {cantidades_producto} unidades.\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c2c78c2c", + "metadata": {}, + "outputs": [], + "source": [ + "def precio_total (orden_cliente1, precios):\n", + " for producto, cantidad in orden_cliente1.items():\n", + " total += precios[producto] * cantidad\n", + " return total" + ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, @@ -90,7 +130,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.9" } }, "nbformat": 4, From ff32f97692271c01c17faee37266f12985cfc802 Mon Sep 17 00:00:00 2001 From: Lautaro Date: Thu, 15 Jan 2026 14:51:25 +0100 Subject: [PATCH 2/4] 4.2 --- lab-python-error-handling.ipynb | 38 ++++++++++++++++++++++++--------- 1 file changed, 28 insertions(+), 10 deletions(-) diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index 9f9cd23..6805e0a 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -75,7 +75,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 20, "id": "5a77a67b", "metadata": {}, "outputs": [], @@ -85,19 +85,18 @@ " inventario = {}\n", " for producto in productos:\n", " cantidad_valida = False\n", - " while not cantidad_valida: \n", + " while not cantidad_valida:\n", " try:\n", " cantidades_producto = int(input(f\"Ingrese la cantidad inicial de {producto}: \"))\n", " if cantidades_producto < 0:\n", " print(\"Por favor, ingrese un número entero no negativo.\")\n", " else:\n", + " inventario[producto] = cantidades_producto\n", " cantidad_valida = True\n", - " return inventario\n", " except ValueError:\n", " print(\"Ha ocurrido un error. Por favor, ingrese un número entero.\")\n", - " finally:\n", - " inventario[producto] = cantidades_producto\n", - " print (f\"Inventario inicial de {producto}: {cantidades_producto} unidades.\")" + " print(f\"Inventario inicial de {producto}: {inventario[producto]} unidades.\")\n", + " return inventario" ] }, { @@ -105,12 +104,31 @@ "execution_count": null, "id": "c2c78c2c", "metadata": {}, - "outputs": [], + "outputs": [ + { + "ename": "IndentationError", + "evalue": "expected an indented block after 'if' statement on line 5 (2439093952.py, line 6)", + "output_type": "error", + "traceback": [ + " \u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[22]\u001b[39m\u001b[32m, line 6\u001b[39m\n\u001b[31m \u001b[39m\u001b[31mreturn precio_unitario[producto] * cantidad\u001b[39m\n ^\n\u001b[31mIndentationError\u001b[39m\u001b[31m:\u001b[39m expected an indented block after 'if' statement on line 5\n" + ] + } + ], "source": [ - "def precio_total (orden_cliente1, precios):\n", + "\n", + "try:\n", " for producto, cantidad in orden_cliente1.items():\n", - " total += precios[producto] * cantidad\n", - " return total" + " if precio_unitario > 0:\n", + " return precio_unitario[producto] * cantidad\n", + "except ValueError:\n", + " print(\"Ha ocurrido un error. El precio unitario debe ser un número entero no negativo.\")\n", + "except TypeError:\n", + " print(\"Ha ocurrido un error. El precio unitario debe ser un número entero.\")\n", + "finally:\n", + " print(\"Cálculo de precio total finalizado.\")\n", + " precio_total = sum(precio_unitario.values())\n", + " \n", + "print (f\"El precio total de la orden es: ${precio_total}\")" ] } ], From c5fbc7d6c7b0845810ee3a11f7185bcb2326f9b2 Mon Sep 17 00:00:00 2001 From: Lautaro Date: Thu, 15 Jan 2026 15:02:46 +0100 Subject: [PATCH 3/4] 4.3 --- lab-python-error-handling.ipynb | 58 ++++++++++++++++++--------------- 1 file changed, 32 insertions(+), 26 deletions(-) diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index 6805e0a..53deae6 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -75,7 +75,7 @@ }, { "cell_type": "code", - "execution_count": 20, + "execution_count": 27, "id": "5a77a67b", "metadata": {}, "outputs": [], @@ -101,34 +101,40 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 28, "id": "c2c78c2c", "metadata": {}, - "outputs": [ - { - "ename": "IndentationError", - "evalue": "expected an indented block after 'if' statement on line 5 (2439093952.py, line 6)", - "output_type": "error", - "traceback": [ - " \u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[22]\u001b[39m\u001b[32m, line 6\u001b[39m\n\u001b[31m \u001b[39m\u001b[31mreturn precio_unitario[producto] * cantidad\u001b[39m\n ^\n\u001b[31mIndentationError\u001b[39m\u001b[31m:\u001b[39m expected an indented block after 'if' statement on line 5\n" - ] - } - ], + "outputs": [], "source": [ - "\n", - "try:\n", - " for producto, cantidad in orden_cliente1.items():\n", - " if precio_unitario > 0:\n", - " return precio_unitario[producto] * cantidad\n", - "except ValueError:\n", - " print(\"Ha ocurrido un error. El precio unitario debe ser un número entero no negativo.\")\n", - "except TypeError:\n", - " print(\"Ha ocurrido un error. El precio unitario debe ser un número entero.\")\n", - "finally:\n", - " print(\"Cálculo de precio total finalizado.\")\n", - " precio_total = sum(precio_unitario.values())\n", - " \n", - "print (f\"El precio total de la orden es: ${precio_total}\")" + "def calcular_precio_total (orden_cliente1):\n", + " precio_total = 0\n", + " for item in orden_cliente1:\n", + " try:\n", + " precio_total += item['precio'] * item['cantidad']\n", + " except KeyError as e:\n", + " print(f\"Error: falta la clave {e} en el ítem {item}.\")\n", + " except TypeError:\n", + " print(f\"Error: el ítem {item} no es un diccionario válido.\")\n", + " return precio_total" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "id": "8b5bb496", + "metadata": {}, + "outputs": [], + "source": [ + "def obtener_orden_cliente ():\n", + " orden_cliente = set ()\n", + " try:\n", + " producto = input(\"Ingrese el nombre del producto que desea comprar (o 'fin' para terminar): \")\n", + " except ListwrongType as e:\n", + " print(f\"Error: {e}. Por favor, ingrese un nombre de producto válido.\")\n", + " except:\n", + " print (\"Ha ocurrido un error inesperado. Por favor, intente nuevamente.\")\n", + " finally:\n", + " print(\"Gracias por utilizar nuestro sistema de pedidos.\")" ] } ], From 963a1f732c71b906141ee50ede0b8d76c82ae632 Mon Sep 17 00:00:00 2001 From: Lautaro Date: Thu, 15 Jan 2026 15:36:04 +0100 Subject: [PATCH 4/4] 4.3 --- lab-python-error-handling.ipynb | 129 ++++++++++++++++++++++++++------ 1 file changed, 106 insertions(+), 23 deletions(-) diff --git a/lab-python-error-handling.ipynb b/lab-python-error-handling.ipynb index 53deae6..fe16ace 100644 --- a/lab-python-error-handling.ipynb +++ b/lab-python-error-handling.ipynb @@ -75,7 +75,7 @@ }, { "cell_type": "code", - "execution_count": 27, + "execution_count": 41, "id": "5a77a67b", "metadata": {}, "outputs": [], @@ -101,41 +101,124 @@ }, { "cell_type": "code", - "execution_count": 28, + "execution_count": 43, + "id": "450ff0d8", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Inventario inicial de t-shirts: 4 unidades.\n", + "Por favor, ingrese un número entero no negativo.\n", + "Inventario inicial de mugs: 2 unidades.\n", + "Ha ocurrido un error. Por favor, ingrese un número entero.\n", + "Inventario inicial de hats: 5 unidades.\n", + "Inventario inicial de books: 10 unidades.\n", + "Ha ocurrido un error. Por favor, ingrese un número entero.\n", + "Inventario inicial de keychains: 1 unidades.\n", + "Inventario inicial completo: {'t-shirts': 4, 'mugs': 2, 'hats': 5, 'books': 10, 'keychains': 1}\n" + ] + } + ], + "source": [ + "productos = [\"t-shirts\", \"mugs\", \"hats\", \"books\", \"keychains\"]\n", + "inventario = inventario_inicial(productos)\n", + "print(\"Inventario inicial completo:\", inventario)\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, "id": "c2c78c2c", "metadata": {}, "outputs": [], "source": [ - "def calcular_precio_total (orden_cliente1):\n", - " precio_total = 0\n", - " for item in orden_cliente1:\n", - " try:\n", - " precio_total += item['precio'] * item['cantidad']\n", - " except KeyError as e:\n", - " print(f\"Error: falta la clave {e} en el ítem {item}.\")\n", - " except TypeError:\n", - " print(f\"Error: el ítem {item} no es un diccionario válido.\")\n", - " return precio_total" + "def calcular_precio_total(productos):\n", + " total = 0\n", + " for producto in productos:\n", + " while True:\n", + " try:\n", + " precio = float(input(f\"Ingrese el precio de {producto}: \"))\n", + " if precio < 0:\n", + " print(\"El precio no puede ser negativo.\")\n", + " else:\n", + " total += precio\n", + " break\n", + " except ValueError:\n", + " print(\"Por favor, ingrese un precio válido.\")\n", + " return total" ] }, { "cell_type": "code", - "execution_count": 29, + "execution_count": 18, + "id": "02ac22dd", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "calcular_precio_total() missing 1 required positional argument: 'precios'", + "output_type": "error", + "traceback": [ + "\u001b[31m---------------------------------------------------------------------------\u001b[39m", + "\u001b[31mTypeError\u001b[39m Traceback (most recent call last)", + "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[18]\u001b[39m\u001b[32m, line 8\u001b[39m\n\u001b[32m 1\u001b[39m orden_cliente1 = {\n\u001b[32m 2\u001b[39m \u001b[33m\"\u001b[39m\u001b[33mt-shirts\u001b[39m\u001b[33m\"\u001b[39m: \u001b[32m2\u001b[39m,\n\u001b[32m 3\u001b[39m \u001b[33m\"\u001b[39m\u001b[33mmugs\u001b[39m\u001b[33m\"\u001b[39m: \u001b[32m1\u001b[39m,\n\u001b[32m (...)\u001b[39m\u001b[32m 6\u001b[39m \u001b[33m\"\u001b[39m\u001b[33mkeychains\u001b[39m\u001b[33m\"\u001b[39m: \u001b[32m4\u001b[39m\n\u001b[32m 7\u001b[39m }\n\u001b[32m----> \u001b[39m\u001b[32m8\u001b[39m total_orden1 = \u001b[43mcalcular_precio_total\u001b[49m\u001b[43m(\u001b[49m\u001b[43morden_cliente1\u001b[49m\u001b[43m.\u001b[49m\u001b[43mkeys\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m\n\u001b[32m 9\u001b[39m \u001b[38;5;28mprint\u001b[39m(\u001b[33mf\u001b[39m\u001b[33m\"\u001b[39m\u001b[33mEl precio total de la orden del cliente 1 es: $\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mtotal_orden1\u001b[38;5;132;01m:\u001b[39;00m\u001b[33m.2f\u001b[39m\u001b[38;5;132;01m}\u001b[39;00m\u001b[33m\"\u001b[39m)\n", + "\u001b[31mTypeError\u001b[39m: calcular_precio_total() missing 1 required positional argument: 'precios'" + ] + } + ], + "source": [ + "orden_cliente1 = {\n", + " \"t-shirts\": 2,\n", + " \"mugs\": 1,\n", + " \"hats\": 3,\n", + " \"books\": 1,\n", + " \"keychains\": 4\n", + "}\n", + "total_orden1 = calcular_precio_total(orden_cliente1.keys())\n", + "print(f\"El precio total de la orden del cliente 1 es: ${total_orden1:.2f}\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": 38, "id": "8b5bb496", "metadata": {}, "outputs": [], "source": [ - "def obtener_orden_cliente ():\n", - " orden_cliente = set ()\n", - " try:\n", - " producto = input(\"Ingrese el nombre del producto que desea comprar (o 'fin' para terminar): \")\n", - " except ListwrongType as e:\n", - " print(f\"Error: {e}. Por favor, ingrese un nombre de producto válido.\")\n", - " except:\n", - " print (\"Ha ocurrido un error inesperado. Por favor, intente nuevamente.\")\n", - " finally:\n", - " print(\"Gracias por utilizar nuestro sistema de pedidos.\")" + "def obtener_orden_cliente (inventario):\n", + " while True:\n", + " try:\n", + " num = int(input(\"¿Cuántos pedidos desea realizar?: \"))\n", + " if num < 0:\n", + " print(\"Debe ingresar un número no negativo.\")\n", + " else:\n", + " break\n", + " except ValueError:\n", + " print(\"Ingrese un número válido.\")\n", + " pedidos = set()\n", + " for _ in range(num):\n", + " while True:\n", + " producto = input(\"Producto: \")\n", + " try:\n", + " if producto in inventario and inventario[producto] > 0:\n", + " pedidos.add(producto)\n", + " break\n", + " else:\n", + " print(\"Producto no válido o sin stock.\")\n", + " except:\n", + " print(\"Error. Ingrese un producto válido.\")\n", + " return pedidos" ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "19c7d17c", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": {