Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
151 changes: 149 additions & 2 deletions lab-python-error-handling.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,158 @@
"\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": 41,
"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",
" inventario[producto] = cantidades_producto\n",
" cantidad_valida = True\n",
" except ValueError:\n",
" print(\"Ha ocurrido un error. Por favor, ingrese un número entero.\")\n",
" print(f\"Inventario inicial de {producto}: {inventario[producto]} unidades.\")\n",
" return inventario"
]
},
{
"cell_type": "code",
"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(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": 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 (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": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -90,7 +237,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.9"
}
},
"nbformat": 4,
Expand Down