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
181 changes: 177 additions & 4 deletions lab-python-functions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,188 @@
"\n",
"- Consider the input parameters required for each function and their return values.\n",
"- Utilize function parameters and return values to transfer data between functions.\n",
"- Test your functions individually to ensure they work correctly.\n",
"- Test your functions individually to ensure they work correctly."
]
},
{
"cell_type": "code",
"execution_count": 40,
"id": "771ec5e6",
"metadata": {},
"outputs": [],
"source": [
"def initialize_inventory(products):\n",
" \"\"\"\n",
" Inicializa el inventario pidiendo al usuario la cantidad disponible de cada producto.\n",
" Devuelve un diccionario con el formato {producto: cantidad}.\n",
" \"\"\"\n",
" inventory = {}\n",
"\n",
" for product in products:\n",
" cantidad = input(f\"Introduce la cantidad disponible de '{product}': \")\n",
" if not cantidad.isdigit():\n",
" print(\"Cantidad no válida. Se establece en 0.\")\n",
" cantidad = 0\n",
" inventory[product] = int(cantidad)\n",
"\n",
" return inventory\n"
]
},
{
"cell_type": "code",
"execution_count": 41,
"id": "2071dab4",
"metadata": {},
"outputs": [],
"source": [
"def get_customer_orders():\n",
" \"\"\"\n",
" Solicita al usuario los productos que desea pedir.\n",
" Finaliza cuando el usuario escribe 'done'.\n",
" Devuelve un conjunto (set) con los productos pedidos.\n",
" \"\"\"\n",
" customer_orders = set()\n",
"\n",
" while True:\n",
" producto = input(\"Introduce el producto deseado (o 'done' para finalizar): \").lower()\n",
" if producto == \"done\":\n",
" break\n",
" customer_orders.add(producto)\n",
"\n",
" return customer_orders\n"
]
},
{
"cell_type": "code",
"execution_count": 42,
"id": "064c7f31",
"metadata": {},
"outputs": [],
"source": [
"def update_inventory(customer_orders, inventory):\n",
" \"\"\"\n",
" Actualiza el inventario según los pedidos del cliente.\n",
" Si el producto existe y hay stock, resta una unidad por cada pedido.\n",
" Si no hay stock o el producto no existe, se ignora.\n",
" Devuelve el inventario actualizado.\n",
" \"\"\"\n",
" for producto in customer_orders:\n",
" if producto in inventory:\n",
" if inventory[producto] > 0:\n",
" inventory[producto] -= 1\n",
" else:\n",
" print(f\"No hay stock de '{producto}'.\")\n",
" else:\n",
" print(f\"'{producto}' no existe en el inventario.\")\n",
" return inventory\n"
]
},
{
"cell_type": "code",
"execution_count": 43,
"id": "d28df7d6",
"metadata": {},
"outputs": [],
"source": [
"def calculate_order_statistics(customer_orders, products):\n",
" \"\"\"\n",
" Calcula estadísticas básicas de los pedidos:\n",
" - Total de productos pedidos.\n",
" - Porcentaje de productos únicos pedidos respecto al total disponible.\n",
" \"\"\"\n",
" total_pedidos = len(customer_orders)\n",
" total_disponibles = len(products)\n",
" porcentaje_unicos = (total_pedidos / total_disponibles) * 100\n",
" return total_pedidos, porcentaje_unicos\n"
]
},
{
"cell_type": "code",
"execution_count": 44,
"id": "29e5d669",
"metadata": {},
"outputs": [],
"source": [
"def print_order_statistics(order_statistics):\n",
" \"\"\"\n",
" Imprime las estadísticas del pedido de forma legible.\n",
" \"\"\"\n",
" total_pedidos, porcentaje_unicos = order_statistics\n",
"\n",
" print(\"\\nESTADÍSTICAS DEL PEDIDO\")\n",
" print(\"-\" * 35)\n",
" print(f\"Total de productos pedidos: {total_pedidos}\")\n",
" print(f\"Porcentaje de productos únicos pedidos: {porcentaje_unicos:.2f}%\")\n",
" print(\"-\" * 35)\n"
]
},
{
"cell_type": "code",
"execution_count": 45,
"id": "8455f78a",
"metadata": {},
"outputs": [],
"source": [
"def print_updated_inventory(inventory):\n",
" \"\"\"\n",
" Imprime el inventario actualizado producto por producto.\n",
" \"\"\"\n",
" print(\"\\nINVENTARIO ACTUALIZADO\")\n",
" print(\"-\" * 30)\n",
"\n",
" for producto, cantidad in inventory.items():\n",
" print(f\"- {producto}: {cantidad} unidades\")\n",
"\n",
" print(\"-\" * 30)\n",
" print(\"Fin del inventario.\\n\")\n"
]
},
{
"cell_type": "code",
"execution_count": 46,
"id": "cfb79550",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"'hay' no existe en el inventario.\n",
"\n",
"ESTADÍSTICAS DEL PEDIDO\n",
"-----------------------------------\n",
"Total de productos pedidos: 3\n",
"Porcentaje de productos únicos pedidos: 60.00%\n",
"-----------------------------------\n",
"\n",
"INVENTARIO ACTUALIZADO\n",
"------------------------------\n",
"- t-shirt: 49 unidades\n",
"- mug: 29 unidades\n",
"- hat: 45 unidades\n",
"- book: 60 unidades\n",
"- keychain: 80 unidades\n",
"------------------------------\n",
"Fin del inventario.\n",
"\n"
]
}
],
"source": [
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"\n",
"\n"
"inventory = initialize_inventory(products)\n",
"customer_orders = get_customer_orders()\n",
"updated_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(updated_inventory)\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
Expand All @@ -61,7 +234,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.7"
}
},
"nbformat": 4,
Expand Down