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
84 changes: 82 additions & 2 deletions lab-python-functions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,91 @@
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "19e1fe41",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Order Statistics:\n",
"Total Products Ordered: 1\n",
"Percentage of Products Ordered: 20.0%\n",
"t-shirt: 9\n",
"mug: 7\n",
"hat: 7\n",
"book: 6\n",
"keychain: 5\n"
]
}
],
"source": [
"# Paso 1\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"\n",
"# Paso 2\n",
"def initialize_inventory (products):\n",
" inventory = {}\n",
" for product in products:\n",
" inventory[product] = int(input(f\"Introduce la cantidad de {product}: \"))\n",
" return inventory\n",
"\n",
"# Paso 3\n",
"def get_customer_orders (products):\n",
" customer_orders = set()\n",
" answer = \"yes\"\n",
" while answer == \"yes\":\n",
" product = input(\"Introduce el nombre del producto: \").strip().lower()\n",
" if product in products:\n",
" customer_orders.add(product)\n",
" else:\n",
" print(\"Producto no válido, inténtalo de nuevo por favor\")\n",
" answer = input(\"¿Quieres añadir otro producto? (yes/no): \").lower()\n",
" return customer_orders\n",
"\n",
"# Paso 4\n",
"def update_inventory(customer_orders, inventory):\n",
" for product in customer_orders:\n",
" if product in inventory:\n",
" inventory[product] -= 1\n",
"\n",
" return inventory\n",
"\n",
"# Paso 5\n",
"def calculate_order_statistics(customer_orders, products):\n",
" total_products_ordered = len(customer_orders)\n",
" percentage = (len(customer_orders) / len(products)) * 100\n",
" order_status = (total_products_ordered, percentage)\n",
" return order_status\n",
"\n",
"# Paso 6\n",
"def print_order_statistics(order_statistics):\n",
" total, percentage = order_statistics\n",
" print(\"Order Statistics:\")\n",
" print(f\"Total Products Ordered: {total}\")\n",
" print(f\"Percentage of Products Ordered: {percentage}%\")\n",
"#Paso 7\n",
"def print_updated_inventory(inventory):\n",
" for product, quantity in inventory.items():\n",
" print(f\"{product}: {quantity}\")\n",
"\n",
"#Paso 8: Llamadas a la función.\n",
"inventory = initialize_inventory(products)\n",
"customer_orders = get_customer_orders(products)\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)\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -61,7 +141,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.9"
}
},
"nbformat": 4,
Expand Down