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
165 changes: 163 additions & 2 deletions lab-python-error-handling.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,172 @@
"\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": 39,
"id": "0a2b65e4",
"metadata": {},
"outputs": [],
"source": [
"# inventario!\n",
"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\"Ingrese la cantidad de {product}s disponibles: \"))\n",
" if quantity >= 0:\n",
" inventory[product] = quantity\n",
" valid_input = True\n",
" else:\n",
" print(\"La cantidad no puede ser negativa. Intente nuevamente.\")\n",
" except ValueError:\n",
" print(\"Entrada inválida. Por favor ingrese un número válido.\")\n",
" return inventory\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 40,
"id": "7ae074f8",
"metadata": {},
"outputs": [],
"source": [
"# imput del pedido del cliente\n",
"def get_customer_orders(inventory):\n",
" customer_orders = {}\n",
"\n",
" # Número de productos a pedir\n",
" valid_input = False\n",
" while not valid_input:\n",
" try:\n",
" num_orders = int(input(\"¿Cuántos productos desea pedir?: \"))\n",
" if num_orders > 0:\n",
" valid_input = True\n",
" else:\n",
" print(\"El número debe ser mayor que cero.\")\n",
" except ValueError:\n",
" print(\"Entrada inválida. Por favor ingrese un número válido.\")\n",
"\n",
" # Pedidos\n",
" for _ in range(num_orders):\n",
" valid_product = False\n",
" while not valid_product:\n",
" product = input(\"Ingrese el nombre del producto: \").lower()\n",
"\n",
" if product not in inventory:\n",
" print(\"El producto no existe. Intente nuevamente.\")\n",
" elif inventory[product] <= 0:\n",
" print(\"No hay stock disponible de este producto.\")\n",
" else:\n",
" customer_orders[product] = customer_orders.get(product, 0) + 1\n",
" inventory[product] -= 1\n",
" valid_product = True\n",
"\n",
" return customer_orders\n"
]
},
{
"cell_type": "code",
"execution_count": 41,
"id": "59b038fc",
"metadata": {},
"outputs": [],
"source": [
"# calculo del total\n",
"def calculate_total_price(customer_orders):\n",
" total_price = 0\n",
"\n",
" for product, quantity in customer_orders.items():\n",
" valid_input = False\n",
" while not valid_input:\n",
" try:\n",
" price = float(input(f\"Ingrese el precio unitario de {product}: \"))\n",
" if price >= 0:\n",
" total_price += price * quantity\n",
" valid_input = True\n",
" else:\n",
" print(\"El precio no puede ser negativo.\")\n",
" except ValueError:\n",
" print(\"Entrada inválida. Ingrese un número válido.\")\n",
"\n",
" return total_price\n"
]
},
{
"cell_type": "code",
"execution_count": 42,
"id": "10168b8d",
"metadata": {},
"outputs": [],
"source": [
"# parte principal del programa\n",
"def main():\n",
" products = [\"apple\", \"banana\", \"orange\"]\n",
"\n",
" print(\"=== Inicialización del Inventario ===\")\n",
" inventory = initialize_inventory(products)\n",
"\n",
" print(\"\\nInventario cargado:\")\n",
" for product, quantity in inventory.items():\n",
" print(f\"{product}: {quantity}\")\n",
"\n",
" print(\"\\n=== Toma de Pedido del Cliente ===\")\n",
" customer_orders = get_customer_orders(inventory)\n",
"\n",
" print(\"\\n=== Cálculo del Precio Total ===\")\n",
" total = calculate_total_price(customer_orders)\n",
"\n",
" print(\"\\n=== Resumen del Pedido ===\")\n",
" for product, quantity in customer_orders.items():\n",
" print(f\"{product}: {quantity}\")\n",
"\n",
" print(f\"\\nPrecio total del pedido: €{total:.2f}\")\n"
]
},
{
"cell_type": "code",
"execution_count": 43,
"id": "06bd4c64",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"=== Inicialización del Inventario ===\n",
"La cantidad no puede ser negativa. Intente nuevamente.\n",
"La cantidad no puede ser negativa. Intente nuevamente.\n",
"La cantidad no puede ser negativa. Intente nuevamente.\n",
"La cantidad no puede ser negativa. Intente nuevamente.\n",
"\n",
"Inventario cargado:\n",
"apple: 2\n",
"banana: 2\n",
"orange: 2\n",
"\n",
"=== Toma de Pedido del Cliente ===\n",
"\n",
"=== Cálculo del Precio Total ===\n",
"\n",
"=== Resumen del Pedido ===\n",
"orange: 1\n",
"\n",
"Precio total del pedido: €1.00\n"
]
}
],
"source": [
"main()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
Expand All @@ -90,7 +251,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.14.0"
}
},
"nbformat": 4,
Expand Down