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
183 changes: 181 additions & 2 deletions lab-python-list-comprehension.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,190 @@
"\n",
"```\n"
]
},
{
"cell_type": "markdown",
"id": "1767f4ea",
"metadata": {},
"source": [
"1. Review your code from the previous exercise and identify areas where you can apply comprehension to simplify and streamline your code."
]
},
{
"cell_type": "code",
"execution_count": 54,
"id": "bb6df10a",
"metadata": {},
"outputs": [],
"source": [
"# Creamos una lista de productos\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]"
]
},
{
"cell_type": "code",
"execution_count": 55,
"id": "e6f3017b",
"metadata": {},
"outputs": [],
"source": [
"# Comprimimos la funcion initialize_inventory\n",
"def initialize_inventory(products):\n",
" return {product: int(input(f\"Enter the quantity of {product}s available: \")) for product in products}"
]
},
{
"cell_type": "markdown",
"id": "ba8d4de9",
"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."
]
},
{
"cell_type": "code",
"execution_count": 56,
"id": "a8743414",
"metadata": {},
"outputs": [],
"source": [
"# Comprimimos la funcion get_customer_order\n",
"def get_customer_order(inventory):\n",
" num_orders = int(input(\"Enter the number of customer orders: \"))\n",
" orders = [input(\"Enter the product name of a product that a customer wants to order: \").strip().lower() for _ in range(num_orders)]\n",
" return [order for order in orders if order in inventory], num_orders\n"
]
},
{
"cell_type": "markdown",
"id": "e5241541",
"metadata": {},
"source": [
"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."
]
},
{
"cell_type": "code",
"execution_count": 57,
"id": "978c77b2",
"metadata": {},
"outputs": [],
"source": [
"# Añadimos una nueva funcion para calcular precios\n",
"def calculate_total_price(customer_orders):\n",
" total = 0\n",
" for product in customer_orders:\n",
" price = float(input(f\"Enter the price of {product}: \"))\n",
" total += price\n",
" print(f\"Total Price: {total:}\")\n",
" return total"
]
},
{
"cell_type": "markdown",
"id": "f95c3b5e",
"metadata": {},
"source": [
"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."
]
},
{
"cell_type": "code",
"execution_count": 58,
"id": "a1e2df07",
"metadata": {},
"outputs": [],
"source": [
"# Comprimos la funcion update_inventory\n",
"def update_inventory(inventory, customer_orders):\n",
" new_inventory = {product: quantity-1 if product in customer_orders else quantity for product, quantity in inventory.items() if quantity-1 > 0 or product not in customer_orders}\n",
" print(\"Udpate Inventory:\")\n",
" for product, quantity in new_inventory.items():\n",
" print(f\"{product}: {quantity}\")\n",
" return new_inventory"
]
},
{
"cell_type": "markdown",
"id": "500026a1",
"metadata": {},
"source": [
"5. Print the total price of the customer order.\n",
"\n",
"Your code should produce output similar to the following:\n",
"\n",
"```python\n",
"Enter the quantity of t-shirts available: 5\n",
"Enter the quantity of mugs available: 4\n",
"Enter the quantity of hats available: 3\n",
"Enter the quantity of books available: 2\n",
"Enter the quantity of keychains available: 1\n",
"Enter the number of customer orders: 2\n",
"Enter the name of a product that a customer wants to order: hat\n",
"Enter the name of a product that a customer wants to order: keychain\n",
"\n",
"Order Statistics:\n",
"Total Products Ordered: 2\n",
"Percentage of Unique Products Ordered: 40.0\n",
"\n",
"Updated Inventory:\n",
"t-shirt: 5\n",
"mug: 4\n",
"hat: 2\n",
"book: 2\n",
"Enter the price of keychain: 5\n",
"Enter the price of hat: 10\n",
"Total Price: 15.0"
]
},
{
"cell_type": "code",
"execution_count": 59,
"id": "45f649c7",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Order Statistics: \n",
"Total Products Ordered: 2\n",
"Percentage of Unique Products Ordered: 40.0%\n",
"Udpate Inventory:\n",
"t-shirt: 5\n",
"mug: 4\n",
"hat: 2\n",
"book: 2\n",
"Total Price: 15.0\n"
]
}
],
"source": [
"\n",
"def main():\n",
" products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"\n",
" inventory = initialize_inventory(products)\n",
" customer_orders, num_orders = get_customer_order(inventory)\n",
"\n",
" print(\"\\nOrder Statistics: \")\n",
" print(f\"Total Products Ordered: {len(customer_orders)}\")\n",
" percentage = (len(customer_orders) / len(inventory)) * 100\n",
" print(f\"Percentage of Unique Products Ordered: {percentage:}%\")\n",
"\n",
" update_inventory(inventory, customer_orders)\n",
" calculate_total_price(customer_orders)\n",
"\n",
"if __name__ == \"__main__\":\n",
" main()\n",
" "
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
Expand All @@ -93,7 +272,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.12.9"
}
},
"nbformat": 4,
Expand Down