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
78 changes: 76 additions & 2 deletions lab-python-list-comprehension.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,85 @@
"\n",
"```\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "fb03d486",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Inventario: {'t-shirt': 1, 'mug': 1, 'hat': 1, 'book': 1, 'keychain': 1}\n",
"Number of orders: 1\n",
"Customer Orders: {'hat'}\n",
"Updated Inventory: {'t-shirt': 1, 'mug': 1, 'book': 1, 'keychain': 1}\n",
"Total Price: 5.0 EUR\n"
]
}
],
"source": [
"#1. Review your code from the previous exercise and identify areas where you can apply comprehension to simplify and streamline your code.\n",
"#*Hint: Apply it to initialize inventory, updating the inventory and printing the updated inventory.*\n",
"\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"\n",
"def initialize_inventory(products):\n",
" inventory = {product: int(input(f\"Enter the quantity of {product}s available: \")) for product in products}\n",
" return inventory\n",
"\n",
"inventory = initialize_inventory(products)\n",
"print(\"Inventario: \", inventory)\n",
"\n",
"\n",
"#2. Modify the function get_customer_orders\n",
"#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",
"\n",
"def get_customer_orders():\n",
" quantity_of_orders = int(input(\"Enter the number of customer orders:\")) \n",
" print(\"Number of orders: \", quantity_of_orders)\n",
" customer_orders = [input(\"Enter a product to order (t-shirt, mug, hat, book, keychain):\").strip().lower() for x in range(quantity_of_orders)]\n",
" customer_orders = {order for order in customer_orders if order in products}\n",
" return customer_orders\n",
"\n",
"customer_orders = get_customer_orders()\n",
"print(\"Customer Orders: \", customer_orders)\n",
"\n",
"\n",
"#3. Add a new function to calculate the total price of the customer order. \n",
"# For each product in customer_orders, prompt the user to enter the price of that product. Use comprehension to calculate the total price. \n",
"#Note: assume that the user can only have 1 unit of each product.\n",
"\n",
"def calculate_total_price(customer_orders):\n",
" total_price = sum(float(input(f\"Enter the price for {order}: \")) for order in customer_orders)\n",
" return total_price\n",
"\n",
"\n",
"#4. Modify the update_inventory function to remove the product from the inventory if its quantity becomes zero after fulfilling the customer orders. \n",
"# Use comprehension to filter out the products with a quantity of zero from the inventory.\n",
"\n",
"def update_inventory(customer_orders, inventory):\n",
" return {\n",
" key: value - 1 if key in customer_orders else value\n",
" for key, value in inventory.items()\n",
" if (value - 1 if key in customer_orders else value) > 0\n",
" }\n",
"\n",
"inventory = update_inventory(customer_orders, inventory)\n",
"print(\"Updated Inventory: \", inventory)\n",
"\n",
"\n",
"#5. Print the total price of the customer order.\n",
"\n",
"print(\"Total Price: \", calculate_total_price(customer_orders),\"EUR\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
Expand All @@ -93,7 +167,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.12.9"
}
},
"nbformat": 4,
Expand Down