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
80 changes: 78 additions & 2 deletions lab-python-list-comprehension.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,87 @@
"\n",
"```\n"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "26382508",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Order Statistics:\n",
"Total products ordered: 2\n",
"Percentage of unique products ordered: 40.00%\n",
"\n",
"Updated Inventory:\n",
"t-shirt: 10\n",
"mug: 10\n",
"hat: 9\n",
"book: 9\n",
"keychain: 10\n"
]
}
],
"source": [
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"\n",
"def initialize_inventory(products):\n",
" inventory = {}\n",
" for p in products:\n",
" qty = int(input(f\"Enter the quantity of {p}s available: \"))\n",
" inventory[p] = qty\n",
" return inventory\n",
"\n",
"def get_customer_orders():\n",
" customer_orders = set()\n",
" n = int(input(\"Enter the number of customer orders: \"))\n",
" for _ in range(n):\n",
" name = input(\"Enter the name of a product that a customer wants to order: \").strip().lower()\n",
" customer_orders.add(name)\n",
" return customer_orders\n",
"\n",
"def update_inventory(customer_orders, inventory):\n",
" for item in customer_orders:\n",
" if item in inventory and inventory[item] > 0:\n",
" inventory[item] -= 1\n",
" else:\n",
" print(f\"Sorry, {item} is out of stock.\")\n",
" return inventory\n",
"\n",
"def calculate_order_statistics(customer_orders, products):\n",
" # Solo cuentan productos únicos (set)\n",
" total_products_ordered = len(customer_orders)\n",
" percentage_ordered = (total_products_ordered / len(products)) * 100\n",
" return total_products_ordered, percentage_ordered\n",
"\n",
"def print_order_statistics(order_statistics):\n",
" total_products_ordered, percentage_ordered = order_statistics\n",
" print(\"\\nOrder Statistics:\")\n",
" print(f\"Total products ordered: {total_products_ordered}\")\n",
" print(f\"Percentage of unique products ordered: {percentage_ordered:.2f}%\")\n",
"\n",
"def print_updated_inventory(inventory):\n",
" print(\"\\nUpdated Inventory:\")\n",
" for product, qty in inventory.items():\n",
" print(f\"{product}: {qty}\")\n",
"\n",
"# --- Execution sequence ---\n",
"inventory = initialize_inventory(products)\n",
"customer_orders = get_customer_orders()\n",
"inventory = update_inventory(customer_orders, inventory)\n",
"stats = calculate_order_statistics(customer_orders, products)\n",
"print_order_statistics(stats)\n",
"print_updated_inventory(inventory)\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
Expand All @@ -93,7 +169,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.12.9"
}
},
"nbformat": 4,
Expand Down