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": 1,
"id": "64996751",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Ingrese la cantidad disponible para cada producto:\n",
"\n",
"Order Statistics:\n",
"Total Products Ordered: 1\n",
"Percentage of Products Ordered: 20.00%\n",
"\n",
"Updated Inventory:\n",
"t-shirt: 5\n",
"mug: 5\n",
"hat: 4\n",
"book: 5\n",
"keychain: 5\n"
]
}
],
"source": [
"# Lista de productos\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"\n",
"\n",
"def initialize_inventory(products):\n",
" \"\"\"Inicializa el inventario con cantidades ingresadas por el usuario.\"\"\"\n",
" inventory = {}\n",
" print(\"Ingrese la cantidad disponible para cada producto:\")\n",
" for product in products:\n",
" while True:\n",
" try:\n",
" quantity = int(input(f\"{product}: \"))\n",
" if quantity < 0:\n",
" print(\"La cantidad no puede ser negativa.\")\n",
" continue\n",
" inventory[product] = quantity\n",
" break\n",
" except ValueError:\n",
" print(\"Por favor ingrese un número entero.\")\n",
" return inventory\n",
"\n",
"\n",
"def get_customer_orders():\n",
" \"\"\"Solicita al usuario los productos que desea ordenar y devuelve un set con ellos.\"\"\"\n",
" customer_orders = set()\n",
" while True:\n",
" product = input(\"Ingrese el producto que desea ordenar: \").strip()\n",
" customer_orders.add(product)\n",
" another = input(\"¿Desea agregar otro producto? (sí/no): \").strip().lower()\n",
" if another != 'sí':\n",
" break\n",
" return customer_orders\n",
"\n",
"\n",
"def update_inventory(customer_orders, inventory):\n",
" \"\"\"Actualiza el inventario solo para los productos que se ordenaron.\"\"\"\n",
" for product in customer_orders:\n",
" if product in inventory and inventory[product] > 0:\n",
" inventory[product] -= 1\n",
"\n",
"\n",
"def calculate_order_statistics(customer_orders, products):\n",
" \"\"\"Calcula estadísticas de la orden.\"\"\"\n",
" total_ordered = len(customer_orders)\n",
" percentage_ordered = (total_ordered / len(products)) * 100\n",
" return total_ordered, percentage_ordered\n",
"\n",
"\n",
"def print_order_statistics(order_statistics):\n",
" \"\"\"Imprime las estadísticas de la orden.\"\"\"\n",
" total_ordered, percentage_ordered = order_statistics\n",
" print(\"\\nOrder Statistics:\")\n",
" print(f\"Total Products Ordered: {total_ordered}\")\n",
" print(f\"Percentage of Products Ordered: {percentage_ordered:.2f}%\")\n",
"\n",
"\n",
"def print_updated_inventory(inventory):\n",
" \"\"\"Imprime el inventario actualizado.\"\"\"\n",
" print(\"\\nUpdated Inventory:\")\n",
" for product, quantity in inventory.items():\n",
" print(f\"{product}: {quantity}\")\n",
"\n",
"\n",
"# ---- Programa principal ----\n",
"inventory = initialize_inventory(products)\n",
"customer_orders = get_customer_orders()\n",
"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)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "ab273c12",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Ejercicios Finales:\n"
]
}
],
"source": [
"# Añadidos ejercicios finales\n",
"print(\"\\nEjercicios Finales:\")"
]
}
],
"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.12.9"
}
},
"nbformat": 4,
Expand Down