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
145 changes: 143 additions & 2 deletions lab-python-list-comprehension.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,152 @@
"\n",
"```\n"
]
},
{
"cell_type": "markdown",
"id": "c11a4926",
"metadata": {},
"source": [
" ```python\n",
" def initialize_inventory(products):\n",
" inventory[i] = {product: int(input(f\"Enter the quantity of {product}s available: \")) for product in products}\n",
" return inventory\n",
"\n",
" ```"
]
},
{
"cell_type": "code",
"execution_count": 25,
"id": "a60b390a",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'t-shirt': 5, 'mug': 5, 'hat': 5, 'book': 5, 'keychain': 5}\n"
]
}
],
"source": [
"products= [\"t-shirt\",\"mug\",\"hat\",\"book\",\"keychain\"] #global variable can call inside\n",
"\n",
"def initialize_inventory (products):\n",
" inventory = {i : int(input(f\"Enter the quantity of {i}s available\")) for i in products}\n",
" return inventory\n",
"#como la variable esta dentro de la funcion, tienes que llamar a la funcion como la variable para poder tenerlo. \n",
"inventory = initialize_inventory(products)\n",
"print(inventory)"
]
},
{
"cell_type": "markdown",
"id": "01e670f9",
"metadata": {},
"source": [
"2. Modify the function get_customer_orders so it prompts the user to enter the number of customer orders and gathers the product names using a loop and user input. Use comprehension.\n"
]
},
{
"cell_type": "code",
"execution_count": 54,
"id": "303f3bb1",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'book': 1, 'mug': 1, 'hat': 1}\n"
]
}
],
"source": [
"def get_customer_orders(products):\n",
" n=int(input(f\"How many products do you want? \").strip()) \n",
" entries = [input(f\"Enter your products {i+1}: \").strip().lower() for i in range(n)]\n",
" return {p: entries.count(p) for p in set(entries)}\n",
"customer_orders= get_customer_orders(products)\n",
"print(customer_orders)"
]
},
{
"cell_type": "markdown",
"id": "97a54d30",
"metadata": {},
"source": [
"Add a new function to calculate the total price of the customer order. For each product in customer_orders, prompt the user to enter the price of that product. Use comprehension to calculate the total price. Note: assume that the user can only have 1 unit of each product.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "882eb98b",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"52.04\n"
]
}
],
"source": [
"def price (customer_orders):\n",
" total= sum(float(input(f\"Enter the price of {item}, euros and cents\"))for item in customer_orders)\n",
" return round(total,2)\n",
"total_price=price(customer_orders)\n",
"print(total_price)"
]
},
{
"cell_type": "markdown",
"id": "77db510b",
"metadata": {},
"source": [
"Modify the update_inventory function to remove the product from the inventory if its quantity becomes zero after fulfilling the customer orders. Use comprehension to filter out the products with a quantity of zero from the inventory."
]
},
{
"cell_type": "code",
"execution_count": 68,
"id": "2d473a31",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Uptdated inventory: {'t-shirt': 5, 'mug': 4, 'hat': 4, 'book': 0, 'keychain': 5}\n"
]
}
],
"source": [
"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",
" \n",
" inventory = {key: product for key, product in inventory.items() if product > 0}\n",
" return inventory\n",
"update_inventory(customer_orders, inventory)\n",
"print(\"Uptdated inventory:\", inventory)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "28495f9d",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -93,7 +234,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.5"
}
},
"nbformat": 4,
Expand Down