Skip to content
Open
Show file tree
Hide file tree
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
321 changes: 321 additions & 0 deletions .ipynb_checkpoints/lab-python-list-comprehension-checkpoint.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,321 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "25d7736c-ba17-4aff-b6bb-66eba20fbf4e",
"metadata": {},
"source": [
"# Lab | List, Dict and Set Comprehension"
]
},
{
"cell_type": "markdown",
"id": "7dd3cbde-675a-4b81-92c3-f728846dbe06",
"metadata": {},
"source": [
"## Exercise: Managing Customer Orders Optimized with Comprehension"
]
},
{
"cell_type": "markdown",
"id": "5d500160-2fb7-4777-b5e4-09d45ebaf328",
"metadata": {},
"source": [
"In the previous exercise, you developed a program to manage customer orders and inventory. Now, let's take it a step further and incorporate comprehension into your code.\n",
"\n",
"Follow the steps below to complete the exercise:\n",
"\n",
"1. Review your code from the previous exercise and identify areas where you can apply comprehension to simplify and streamline your code. \n",
"\n",
" - *Hint: Apply it to initialize inventory, updating the inventory and printing the updated inventory.*\n",
" \n",
" - For example, in initializing the inventory, we could have:\n",
" \n",
" ```python\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",
" ```\n",
"<br>\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",
"\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",
"\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",
"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\n",
"\n",
"```\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "3088a610-0510-4849-82e3-884a8be5bb71",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the quantity of t-shirts available: 5\n",
"Enter the quantity of mugs available: 5\n",
"Enter the quantity of hats available: 5\n",
"Enter the quantity of books available: 5\n",
"Enter the quantity of keychains available: 5\n"
]
}
],
"source": [
"#1\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"inventory = {product: int(input(f\"Enter the quantity of {product}s available: \")) for product in products}\n"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "b88c0ca8-8440-4918-98c7-12aaf57c08a9",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the number of customer orders: 3\n",
"Enter the name of product 1 to order: hat\n",
"Enter the name of product 2 to order: mug\n",
"Enter the name of product 3 to order: book\n"
]
}
],
"source": [
"#2\n",
"num_orders = int(input(\"Enter the number of customer orders: \"))\n",
"customer_orders = {input(f\"Enter the name of product {i + 1} to order: \") for i in range(num_orders)}\n"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "abd9de57-158f-4a36-b59b-2b9ba2b3751b",
"metadata": {},
"outputs": [],
"source": [
"#3\n",
"def calculate_total_price(customer_orders):\n",
" return sum(float(input(f\"Enter the price of {item}: \")) for item in customer_orders)\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "05fd9d53-dce3-42cb-a1b9-501616eac0c6",
"metadata": {},
"outputs": [],
"source": [
"#4\n",
"def update_inventory(inventory, customer_orders):\n",
" for item in customer_orders:\n",
" if item in inventory:\n",
" inventory[item] -= 1\n",
" return {item: qty for item, qty in inventory.items() if qty > 0}\n"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "233cd2b0-f755-448c-8231-567c2a184227",
"metadata": {},
"outputs": [],
"source": [
"#5\n",
"def calculate_order_statistics(customer_orders, products):\n",
" total_products_ordered = len(customer_orders)\n",
" percentage_ordered = (total_products_ordered / len(products)) * 100\n",
" return total_products_ordered, percentage_ordered\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "7227ce44-bbaa-45d9-952b-dcca68c5e220",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the quantity of t-shirts available: 5\n",
"Enter the quantity of mugs available: 4\n",
"Enter the quantity of hats available: 6\n",
"Enter the quantity of books available: 5\n",
"Enter the quantity of keychains available: 5\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: book\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\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: 5\n",
"book: 4\n",
"keychain: 5\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the price of hat: 200\n",
"Enter the price of book: 100\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Total Price: 300.00\n"
]
}
],
"source": [
"# --- Step 1: Initialize global product list ---\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"\n",
"\n",
"# --- Step 1: Initialize inventory using dictionary comprehension ---\n",
"def initialize_inventory(products):\n",
" return {product: int(input(f\"Enter the quantity of {product}s available: \")) for product in products}\n",
"\n",
"\n",
"# --- Step 2: Get customer orders using set comprehension ---\n",
"def get_customer_orders(products):\n",
" num_orders = int(input(\"Enter the number of customer orders: \"))\n",
" return {\n",
" prod\n",
" for i in range(num_orders)\n",
" if (prod := input(f\"Enter the name of a product that a customer wants to order: \")) in products\n",
" }\n",
"\n",
"\n",
"# --- Step 3: Calculate total price using comprehension ---\n",
"# (assumes 1 unit per product)\n",
"def calculate_total_price(customer_orders):\n",
" return sum(float(input(f\"Enter the price of {item}: \")) for item in customer_orders)\n",
"\n",
"\n",
"# --- Step 4: Update inventory using comprehension and filter out zero-quantity items ---\n",
"def update_inventory(inventory, customer_orders):\n",
" return {\n",
" item: qty - (1 if item in customer_orders else 0)\n",
" for item, qty in inventory.items()\n",
" if qty - (1 if item in customer_orders else 0) > 0\n",
" }\n",
"\n",
"\n",
"# --- Step 5: Calculate and print order statistics ---\n",
"def calculate_order_statistics(customer_orders, products):\n",
" total_products_ordered = len(customer_orders)\n",
" percentage_ordered = (total_products_ordered / len(products)) * 100\n",
" return total_products_ordered, percentage_ordered\n",
"\n",
"\n",
"def print_order_statistics(stats):\n",
" print(\"\\nOrder Statistics:\")\n",
" print(f\"Total Products Ordered: {stats[0]}\")\n",
" print(f\"Percentage of Unique Products Ordered: {stats[1]:.1f}%\")\n",
"\n",
"\n",
"def print_updated_inventory(inventory):\n",
" print(\"\\nUpdated Inventory:\")\n",
" for item, qty in inventory.items():\n",
" print(f\"{item}: {qty}\")\n",
"\n",
"\n",
"# --- Main Program Flow ---\n",
"inventory = initialize_inventory(products)\n",
"customer_orders = get_customer_orders(products)\n",
"\n",
"updated_inventory = update_inventory(inventory, customer_orders)\n",
"stats = calculate_order_statistics(customer_orders, products)\n",
"\n",
"print_order_statistics(stats)\n",
"print_updated_inventory(updated_inventory)\n",
"\n",
"total_price = calculate_total_price(customer_orders)\n",
"print(f\"\\nTotal Price: {total_price:.2f}\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "316830df-47b3-4813-8c4c-bcde88fd69ad",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.13.5"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading