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
74 changes: 71 additions & 3 deletions lab-python-list-comprehension.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,81 @@
"\n",
"```\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d127f463-6433-4891-8ce4-269b43058393",
"metadata": {},
"outputs": [],
"source": [
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"\n",
"def initialize_inventory(products):\n",
" print(\"Inventory:\")\n",
" inventory = {p: int(input(f\"Enter the number of {p}s available: \")) for p in products}\n",
" return inventory\n",
"\n",
"def get_customer_orders():\n",
" print(\"Customer order:\")\n",
" print(\"Available products:\", products)\n",
" n = int(input(\"Number of customer orders: \"))\n",
" entries = [input(\"Name of a product that a customer wants to order: \").strip().lower()\n",
" for _ in range(n)]\n",
" customer_orders = {name for name in entries if name in products}\n",
" return customer_orders\n",
"\n",
"def update_inventory(customer_orders, inventory):\n",
" updated = {k: (v - 1 if k in customer_orders else v) for k, v in inventory.items()}\n",
" updated = {k: v for k, v in updated.items() if v > 0}\n",
" return updated\n",
"\n",
"def calculate_order_statistics(customer_orders, products):\n",
" total_ordered = len(customer_orders)\n",
" percentage = (total_ordered / len(products)) * 100\n",
" return total_ordered, percentage\n",
"\n",
"def print_order_statistics(order_statistics):\n",
" total_ordered, percentage = order_statistics\n",
" print(\"Order Statistics:\")\n",
" print(\"Total Products Ordered:\", total_ordered)\n",
" print(\"Percentage of Unique Products Ordered:\", percentage)\n",
"\n",
"def print_updated_inventory(inventory):\n",
" print(\"Updated Inventory:\")\n",
" [print(f\"{k}: {v}\") for k, v in inventory.items()] # uso de comprensión para imprimir\n",
"\n",
"def calculate_total_price(customer_orders):\n",
" # Precios por producto con dict comprehension, luego sumamos\n",
" prices = {p: float(input(f\"Enter the price of {p}: \")) for p in customer_orders}\n",
" total = sum(prices.values())\n",
" return total\n",
"\n",
"# Secuencia\n",
"inventory = initialize_inventory(products)\n",
"customer_orders = get_customer_orders()\n",
"stats = calculate_order_statistics(customer_orders, products)\n",
"print_order_statistics(stats)\n",
"inventory = update_inventory(customer_orders, inventory)\n",
"print_updated_inventory(inventory)\n",
"total_price = calculate_total_price(customer_orders)\n",
"print(\"Total Price:\", total_price)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8af600d5-5369-4f64-a9f9-4be10fb60fbc",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python [conda env:base] *",
"language": "python",
"name": "python3"
"name": "conda-base-py"
},
"language_info": {
"codemirror_mode": {
Expand All @@ -93,7 +161,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.5"
}
},
"nbformat": 4,
Expand Down