Skip to content

lab corregido #413

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
172 changes: 170 additions & 2 deletions lab-python-error-handling.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,179 @@
"\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": 6,
"id": "b9f4d63d",
"metadata": {},
"outputs": [],
"source": [
"# Paso 1: Definir la función para inicializar el inventario con manejo de errores\n",
"\n",
"def initialize_inventory(products):\n",
" inventory = {}\n",
" for product in products:\n",
" valid_quantity = False\n",
" while not valid_quantity:\n",
" try:\n",
" quantity = int(input(f\"Ingrese la cantidad disponible de {product}: \"))\n",
" if quantity < 0:\n",
" print(\"La cantidad no puede ser negativa. Intente de nuevo.\")\n",
" else:\n",
" inventory[product] = quantity\n",
" valid_quantity = True\n",
" except ValueError:\n",
" print(\"Entrada inválida. Por favor, ingrese un número entero.\")\n",
" return inventory"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "552da3b9",
"metadata": {},
"outputs": [],
"source": [
"# Paso 2: Modificar la función calculate_total_price para incluir manejo de errores\n",
"\n",
"def calculate_total_price(products):\n",
" prices = {}\n",
" for product in products:\n",
" valid_price = False\n",
" while not valid_price:\n",
" try:\n",
" price = float(input(f\"Enter the price for {product}: \"))\n",
" if price < 0:\n",
" print(\"Price cannot be negative. Please enter a valid price.\")\n",
" else:\n",
" prices[product] = price\n",
" valid_price = True\n",
" except ValueError:\n",
" print(\"Invalid input. Please enter a numeric value for the price.\")\n",
" return prices"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "3ce552b5",
"metadata": {},
"outputs": [],
"source": [
"# Paso 3: Modificar la función get_customer_orders para incluir manejo de errores\n",
"\n",
"def get_customer_orders(inventory):\n",
" orders = {}\n",
" products = list(inventory.keys())\n",
" # Solicitar el número de pedidos\n",
" while True:\n",
" try:\n",
" num_orders = int(input(\"Ingrese el número de pedidos: \"))\n",
" if num_orders < 0:\n",
" print(\"El número de pedidos no puede ser negativo. Intente de nuevo.\")\n",
" else:\n",
" break\n",
" except ValueError:\n",
" print(\"Entrada inválida. Por favor, ingrese un número entero.\")\n",
"\n",
" for _ in range(num_orders):\n",
" while True:\n",
" product = input(f\"Ingrese el nombre del producto ({', '.join(products)}): \")\n",
" if product not in inventory:\n",
" print(\"Producto no encontrado en el inventario. Intente de nuevo.\")\n",
" elif inventory[product] <= 0:\n",
" print(f\"No hay stock disponible para {product}. Elija otro producto.\")\n",
" else:\n",
" break\n",
" while True:\n",
" try:\n",
" quantity = int(input(f\"Ingrese la cantidad de {product} que desea pedir: \"))\n",
" if quantity < 0:\n",
" print(\"La cantidad no puede ser negativa. Intente de nuevo.\")\n",
" elif quantity > inventory[product]:\n",
" print(f\"Cantidad insuficiente en inventario. Solo hay {inventory[product]} disponibles.\")\n",
" else:\n",
" break\n",
" except ValueError:\n",
" print(\"Entrada inválida. Por favor, ingrese un número entero.\")\n",
" orders[product] = orders.get(product, 0) + quantity\n",
" inventory[product] -= quantity\n",
" return orders"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "3c43c0ac",
"metadata": {
"vscode": {
"languageId": "ruby"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Producto no encontrado en el inventario. Intente de nuevo.\n",
"Producto no encontrado en el inventario. Intente de nuevo.\n",
"Producto no encontrado en el inventario. Intente de nuevo.\n",
"Producto no encontrado en el inventario. Intente de nuevo.\n",
"Producto no encontrado en el inventario. Intente de nuevo.\n",
"Producto no encontrado en el inventario. Intente de nuevo.\n",
"Producto no encontrado en el inventario. Intente de nuevo.\n",
"Producto no encontrado en el inventario. Intente de nuevo.\n",
"Producto no encontrado en el inventario. Intente de nuevo.\n",
"Producto no encontrado en el inventario. Intente de nuevo.\n",
"Producto no encontrado en el inventario. Intente de nuevo.\n",
"Producto no encontrado en el inventario. Intente de nuevo.\n",
"Producto no encontrado en el inventario. Intente de nuevo.\n",
"Producto no encontrado en el inventario. Intente de nuevo.\n",
"Producto no encontrado en el inventario. Intente de nuevo.\n",
"Producto no encontrado en el inventario. Intente de nuevo.\n",
"Producto no encontrado en el inventario. Intente de nuevo.\n"
]
},
{
"ename": "KeyboardInterrupt",
"evalue": "Interrupted by user",
"output_type": "error",
"traceback": [
"\u001b[31m---------------------------------------------------------------------------\u001b[39m",
"\u001b[31mKeyboardInterrupt\u001b[39m Traceback (most recent call last)",
"\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[9]\u001b[39m\u001b[32m, line 13\u001b[39m\n\u001b[32m 10\u001b[39m precios = calculate_total_price(productos)\n\u001b[32m 12\u001b[39m \u001b[38;5;66;03m# Obtener pedidos de clientes con manejo de errores\u001b[39;00m\n\u001b[32m---> \u001b[39m\u001b[32m13\u001b[39m pedidos = \u001b[43mget_customer_orders\u001b[49m\u001b[43m(\u001b[49m\u001b[43minventario\u001b[49m\u001b[43m)\u001b[49m\n\u001b[32m 15\u001b[39m \u001b[38;5;28mprint\u001b[39m(\u001b[33m\"\u001b[39m\u001b[33mInventario final:\u001b[39m\u001b[33m\"\u001b[39m, inventario)\n\u001b[32m 16\u001b[39m \u001b[38;5;28mprint\u001b[39m(\u001b[33m\"\u001b[39m\u001b[33mPrecios:\u001b[39m\u001b[33m\"\u001b[39m, precios)\n",
"\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[8]\u001b[39m\u001b[32m, line 19\u001b[39m, in \u001b[36mget_customer_orders\u001b[39m\u001b[34m(inventory)\u001b[39m\n\u001b[32m 17\u001b[39m \u001b[38;5;28;01mfor\u001b[39;00m _ \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28mrange\u001b[39m(num_orders):\n\u001b[32m 18\u001b[39m \u001b[38;5;28;01mwhile\u001b[39;00m \u001b[38;5;28;01mTrue\u001b[39;00m:\n\u001b[32m---> \u001b[39m\u001b[32m19\u001b[39m product = \u001b[38;5;28;43minput\u001b[39;49m\u001b[43m(\u001b[49m\u001b[33;43mf\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43mIngrese el nombre del producto (\u001b[39;49m\u001b[38;5;132;43;01m{\u001b[39;49;00m\u001b[33;43m'\u001b[39;49m\u001b[33;43m, \u001b[39;49m\u001b[33;43m'\u001b[39;49m\u001b[43m.\u001b[49m\u001b[43mjoin\u001b[49m\u001b[43m(\u001b[49m\u001b[43mproducts\u001b[49m\u001b[43m)\u001b[49m\u001b[38;5;132;43;01m}\u001b[39;49;00m\u001b[33;43m): \u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\n\u001b[32m 20\u001b[39m \u001b[38;5;28;01mif\u001b[39;00m product \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;129;01min\u001b[39;00m inventory:\n\u001b[32m 21\u001b[39m \u001b[38;5;28mprint\u001b[39m(\u001b[33m\"\u001b[39m\u001b[33mProducto no encontrado en el inventario. Intente de nuevo.\u001b[39m\u001b[33m\"\u001b[39m)\n",
"\u001b[36mFile \u001b[39m\u001b[32mc:\\Users\\sergi\\OneDrive\\Documentos\\IRONHACK\\venv\\Lib\\site-packages\\ipykernel\\kernelbase.py:1282\u001b[39m, in \u001b[36mKernel.raw_input\u001b[39m\u001b[34m(self, prompt)\u001b[39m\n\u001b[32m 1280\u001b[39m msg = \u001b[33m\"\u001b[39m\u001b[33mraw_input was called, but this frontend does not support input requests.\u001b[39m\u001b[33m\"\u001b[39m\n\u001b[32m 1281\u001b[39m \u001b[38;5;28;01mraise\u001b[39;00m StdinNotImplementedError(msg)\n\u001b[32m-> \u001b[39m\u001b[32m1282\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[43m.\u001b[49m\u001b[43m_input_request\u001b[49m\u001b[43m(\u001b[49m\n\u001b[32m 1283\u001b[39m \u001b[43m \u001b[49m\u001b[38;5;28;43mstr\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mprompt\u001b[49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 1284\u001b[39m \u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m.\u001b[49m\u001b[43m_parent_ident\u001b[49m\u001b[43m[\u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43mshell\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 1285\u001b[39m \u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m.\u001b[49m\u001b[43mget_parent\u001b[49m\u001b[43m(\u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43mshell\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\u001b[43m,\u001b[49m\n\u001b[32m 1286\u001b[39m \u001b[43m \u001b[49m\u001b[43mpassword\u001b[49m\u001b[43m=\u001b[49m\u001b[38;5;28;43;01mFalse\u001b[39;49;00m\u001b[43m,\u001b[49m\n\u001b[32m 1287\u001b[39m \u001b[43m\u001b[49m\u001b[43m)\u001b[49m\n",
"\u001b[36mFile \u001b[39m\u001b[32mc:\\Users\\sergi\\OneDrive\\Documentos\\IRONHACK\\venv\\Lib\\site-packages\\ipykernel\\kernelbase.py:1325\u001b[39m, in \u001b[36mKernel._input_request\u001b[39m\u001b[34m(self, prompt, ident, parent, password)\u001b[39m\n\u001b[32m 1322\u001b[39m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mKeyboardInterrupt\u001b[39;00m:\n\u001b[32m 1323\u001b[39m \u001b[38;5;66;03m# re-raise KeyboardInterrupt, to truncate traceback\u001b[39;00m\n\u001b[32m 1324\u001b[39m msg = \u001b[33m\"\u001b[39m\u001b[33mInterrupted by user\u001b[39m\u001b[33m\"\u001b[39m\n\u001b[32m-> \u001b[39m\u001b[32m1325\u001b[39m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mKeyboardInterrupt\u001b[39;00m(msg) \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mNone\u001b[39;00m\n\u001b[32m 1326\u001b[39m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mException\u001b[39;00m:\n\u001b[32m 1327\u001b[39m \u001b[38;5;28mself\u001b[39m.log.warning(\u001b[33m\"\u001b[39m\u001b[33mInvalid Message:\u001b[39m\u001b[33m\"\u001b[39m, exc_info=\u001b[38;5;28;01mTrue\u001b[39;00m)\n",
"\u001b[31mKeyboardInterrupt\u001b[39m: Interrupted by user"
]
}
],
"source": [
"# Paso 4: Probar el código introduciendo entradas inválidas\n",
"\n",
"# Definir productos de ejemplo\n",
"productos = [\"manzana\", \"banana\", \"naranja\"]\n",
"\n",
"# Inicializar inventario con manejo de errores\n",
"inventario = initialize_inventory(productos)\n",
"\n",
"# Inicializar precios con manejo de errores\n",
"precios = calculate_total_price(productos)\n",
"\n",
"# Obtener pedidos de clientes con manejo de errores\n",
"pedidos = get_customer_orders(inventario)\n",
"\n",
"print(\"Inventario final:\", inventario)\n",
"print(\"Precios:\", precios)\n",
"print(\"Pedidos del cliente:\", pedidos)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "venv",
"language": "python",
"name": "python3"
},
Expand All @@ -90,7 +258,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.3"
}
},
"nbformat": 4,
Expand Down