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
122 changes: 120 additions & 2 deletions lab-python-functions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,129 @@
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "893f4306",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Estadísticas del pedido:\n",
"Total de productos únicos pedidos: 4\n",
"Porcentaje de productos únicos pedidos: 80.00%\n",
"\n",
"Inventario actualizado:\n",
"book: 10\n",
"hat: 9\n",
"keychain: 9\n",
"mug: 9\n",
"t-shirt: 9\n"
]
}
],
"source": [
"# Lista de productos disponibles\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"\n",
"\n",
"# Función auxiliar para pedir un número entero válido\n",
"def get_valid_number(message):\n",
" while True:\n",
" try:\n",
" value = int(input(message))\n",
" return value\n",
" except ValueError:\n",
" print(\"Entrada no válida. Por favor, introduce un número entero.\")\n",
"\n",
"\n",
"# 1. Inicializar inventario\n",
"def initialize_inventory(products):\n",
" inventory = {}\n",
"\n",
" for product in products:\n",
" message = f\"Introduce la cantidad disponible de {product}: \"\n",
" quantity = get_valid_number(message)\n",
" inventory[product] = quantity\n",
"\n",
" return inventory\n",
"\n",
"\n",
"# 2. Obtener pedidos del cliente\n",
"def get_customer_orders(products):\n",
" customer_orders = set()\n",
"\n",
" num_orders = get_valid_number(\"¿Cuántos productos distintos quiere pedir el cliente? \")\n",
"\n",
" while len(customer_orders) < num_orders:\n",
" product = input(\"Introduce el nombre de un producto: \").strip()\n",
"\n",
" if product in products:\n",
" customer_orders.add(product)\n",
" else:\n",
" print(\"Ese producto no está disponible. Inténtalo de nuevo.\")\n",
"\n",
" return customer_orders\n",
"\n",
"\n",
"# 3. Actualizar inventario\n",
"def update_inventory(customer_orders, inventory):\n",
" updated_inventory = inventory.copy()\n",
"\n",
" for product in customer_orders:\n",
" if product in updated_inventory and updated_inventory[product] > 0:\n",
" updated_inventory[product] -= 1\n",
"\n",
" return updated_inventory\n",
"\n",
"\n",
"# 4. Calcular estadísticas del pedido\n",
"def calculate_order_statistics(customer_orders, products):\n",
" total_unique_products_ordered = len(customer_orders)\n",
" total_available_products = len(products)\n",
"\n",
" if total_available_products == 0:\n",
" percentage_ordered = 0\n",
" else:\n",
" percentage_ordered = (total_unique_products_ordered / total_available_products) * 100\n",
"\n",
" return total_unique_products_ordered, percentage_ordered\n",
"\n",
"\n",
"# 5. Imprimir estadísticas del pedido\n",
"def print_order_statistics(order_statistics):\n",
" total_unique_products_ordered, percentage_ordered = order_statistics\n",
"\n",
" print(\"\\nEstadísticas del pedido:\")\n",
" print(f\"Total de productos únicos pedidos: {total_unique_products_ordered}\")\n",
" print(f\"Porcentaje de productos únicos pedidos: {percentage_ordered:.2f}%\")\n",
"\n",
"\n",
"# 6. Imprimir inventario actualizado\n",
"def print_updated_inventory(inventory):\n",
" print(\"\\nInventario actualizado:\")\n",
"\n",
" for product, quantity in sorted(inventory.items()):\n",
" print(f\"{product}: {quantity}\")\n",
"\n",
"\n",
"# 7. Ejecutar programa\n",
"inventory = initialize_inventory(products)\n",
"customer_orders = get_customer_orders(products)\n",
"inventory = update_inventory(customer_orders, inventory)\n",
"order_statistics = calculate_order_statistics(customer_orders, products)\n",
"print_order_statistics(order_statistics)\n",
"print_updated_inventory(inventory)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
Expand All @@ -61,7 +179,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.14.4"
}
},
"nbformat": 4,
Expand Down