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
307 changes: 307 additions & 0 deletions lab-python-list-comprehension.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,307 @@
{
"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": 77,
"id": "aed6a698",
"metadata": {},
"outputs": [],
"source": [
"def initialize_inventory(products):\n",
" inventory = {product: int(input(f\"Enter the quantity of {product}s available: \")) for product in products}\n",
" print(\"Current Inventory:\")\n",
" formatted_items = [f\"Enter the quantity of {product}s available: {quantity}\" for product, quantity in inventory.items()]\n",
" for item in formatted_items:\n",
" print(item)\n",
" return inventory"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5bc8faa6",
"metadata": {},
"outputs": [],
"source": [
"# previously edited code\n",
"def get_customer_orders():\n",
" orders = set()\n",
" while True:\n",
" order = input(\"Enter the name of a product to order: \")\n",
" orders.add(order)\n",
" more = input(\"Do you want to order another product? (yes/no): \")\n",
" if more.lower() != \"yes\":\n",
" break\n",
" return orders"
]
},
{
"cell_type": "code",
"execution_count": 111,
"id": "4471ba80",
"metadata": {},
"outputs": [],
"source": [
"def get_customer_orders():\n",
" num_orders = int(input(\"Enter the number of products you wish to order: \"))\n",
" print(f\"Enter the number of customer orders: {num_orders}\")\n",
" orders = {input(f\"Enter product name {i+1}: \") for i in range(num_orders)}\n",
" formatted_orders = [f\"Enter the name of a product that a customer wants to order: {order}\" for order in sorted(orders)]\n",
" for order in formatted_orders:\n",
" print(order)\n",
" return orders"
]
},
{
"cell_type": "code",
"execution_count": 103,
"id": "0324db36",
"metadata": {},
"outputs": [],
"source": [
"def calculate_order_total_price(customer_orders):\n",
" prices = [int(input(f\"Enter the price of {order}: \")) for order in customer_orders]\n",
" for order, price in zip(customer_orders, prices):\n",
" print(f\"Enter the price of {order}: {price}\")\n",
" total_price = sum(prices)\n",
" print(f\"Total price: {total_price}\")"
]
},
{
"cell_type": "code",
"execution_count": 30,
"id": "1fd68526",
"metadata": {},
"outputs": [],
"source": [
"def update_inventory(customer_orders, inventory):\n",
" updated_inventory = {item:count - list(customer_orders).count(item) if item in customer_orders else count for item, count in inventory.items()}\n",
" return updated_inventory"
]
},
{
"cell_type": "code",
"execution_count": 102,
"id": "f5da5133",
"metadata": {},
"outputs": [],
"source": [
"def calculate_order_statistics(customer_orders, products):\n",
" total_products_ordered = sum(1 for order in customer_orders if order in products)\n",
" total_unique_products = len(products)\n",
" percentage_unique_products_ordered = (total_products_ordered / total_unique_products) * 100\n",
" return total_products_ordered, percentage_unique_products_ordered"
]
},
{
"cell_type": "code",
"execution_count": 96,
"id": "42a80463",
"metadata": {},
"outputs": [],
"source": [
"def print_order_statistics(order_statistics):\n",
" print(\"Order Statistics:\")\n",
" print(f\"\"\"Total products ordered: {order_statistics[0]}\n",
"Percentage of unique products ordered: {order_statistics[1]:.2f}%\"\"\")"
]
},
{
"cell_type": "code",
"execution_count": 105,
"id": "46411eab",
"metadata": {},
"outputs": [],
"source": [
"def print_update_inventory(inventory):\n",
" formated_list = [f\"{product}:{quantity}\"for product, quantity in inventory.items()]\n",
" print(\"Updated Inventory:\")\n",
" for item in formated_list:\n",
" print(item)"
]
},
{
"cell_type": "code",
"execution_count": 26,
"id": "5deeb9e9",
"metadata": {},
"outputs": [],
"source": [
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]"
]
},
{
"cell_type": "code",
"execution_count": 112,
"id": "e2850349",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Current Inventory:\n",
"Enter the quantity of t-shirts available: 10\n",
"Enter the quantity of mugs available: 10\n",
"Enter the quantity of hats available: 10\n",
"Enter the quantity of books available: 10\n",
"Enter the quantity of keychains available: 10\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: mug\n"
]
}
],
"source": [
"inventory = initialize_inventory(products)\n",
"customer_orders = get_customer_orders()"
]
},
{
"cell_type": "code",
"execution_count": 113,
"id": "6f9dd93c",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Order Statistics:\n",
"Total products ordered: 2\n",
"Percentage of unique products ordered: 40.00%\n"
]
}
],
"source": [
"order_statistics = calculate_order_statistics(customer_orders, products)\n",
"print_order_statistics(order_statistics)"
]
},
{
"cell_type": "code",
"execution_count": 114,
"id": "84500ef2",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Updated Inventory:\n",
"t-shirt:10\n",
"mug:9\n",
"hat:9\n",
"book:10\n",
"keychain:10\n",
"Enter the price of hat: 24\n",
"Enter the price of mug: 15\n",
"Total price: 39\n"
]
}
],
"source": [
"updated_inventory = update_inventory(customer_orders, inventory)\n",
"print_update_inventory(updated_inventory)\n",
"calculate_order_total_price(customer_orders)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "base",
"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.9"
}
},
"nbformat": 4,
"nbformat_minor": 5
}