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
112 changes: 110 additions & 2 deletions lab-python-list-comprehension.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"\n",
" ```\n",
"<br>\n",
"\n",
" \n",
" \n",
"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",
Expand Down Expand Up @@ -75,11 +76,118 @@
"\n",
"```\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2effb007",
"metadata": {},
"outputs": [],
"source": [
"#1. Review your code from the previous exercise and identify areas where you can apply comprehension to simplify and streamline your code. \n",
"\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"\n",
"def initialize_inventory(products):\n",
" return {product: int(input(f\"Enter the quantity for {product}: \")) for product in products}\n",
"\n",
"print(initialize_inventory(products))\n",
"\n",
"def get_customer_orders(products):\n",
" orders = set()\n",
" while input(\"Do you want to order a product? (yes/no): \").lower() == \"yes\":\n",
" item = input(\"Enter the product name: \").lower()\n",
" print(f\"{item} added to the order.\") if item in products and not orders.add(item) else print(f\"Product '{item}' is not available.\")\n",
" return orders\n",
"\n",
"\n",
"\n",
"def update_inventory(customer_orders, inventory):\n",
" for item in customer_orders:\n",
" inventory[item] = inventory[item] - 1\n",
" return inventory\n",
"inventory = update_inventory(customer_orders, inventory)\n",
"print(inventory)\n",
"\n",
"def print_updated_inventory(inventory):\n",
" print(\"\\nUpdated Inventory:\")\n",
" for product in inventory:\n",
" print(product + \": \" + str(inventory[product]))\n",
"\n",
"\n",
"\n",
"#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",
"#3. 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",
"#4. 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.\n",
"\n",
"5. Print the total price of the customer order.\n",
"\n",
"\n",
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "ad3a7286",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Inventario: {'t-shirt': 1, 'mug': 1, 'hat': 1, 'book': 1, 'keychain': 1}\n",
"Number of orders: 2\n",
"Customer Orders: {'hat', 'mug'}\n",
"Updated Inventory: {'t-shirt': 1, 'book': 1, 'keychain': 1}\n",
"Total Price: 7.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",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\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",
"inventory = initialize_inventory(products)\n",
"print(\"Inventario: \", inventory)\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",
"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",
"customer_orders = get_customer_orders()\n",
"print(\"Customer Orders: \", customer_orders)\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",
"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",
"#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",
"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",
"inventory = update_inventory(customer_orders, inventory)\n",
"print(\"Updated Inventory: \", inventory)\n",
"#5. Print the total price of the customer order.\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 +201,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.12.9"
}
},
"nbformat": 4,
Expand Down