Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lab work for list, set, and dic comprehension #346

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
293 changes: 293 additions & 0 deletions .ipynb_checkpoints/lab-python-list-comprehension-checkpoint.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,293 @@
{
"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": 1,
"id": "8b14e667-c604-40a6-86bc-0d0305a9d885",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the name of a product that a customer wants to order: book\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"book has been added to your order.\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Do you want to add another product? (yes/no): no\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"One book has been deducted from the inventory.\n",
"\n",
"Updated inventory:\n",
"Book: 9\n",
"Hat: 15\n",
"Keychain: 8\n",
"T-shirt: 5\n",
"Mug: 3\n"
]
}
],
"source": [
"# Sample inventory data (for demonstration)\n",
"inventory = {\n",
" \"book\": 10,\n",
" \"hat\": 15,\n",
" \"keychain\": 8,\n",
" \"t-shirt\": 5,\n",
" \"mug\": 3\n",
"}\n",
"\n",
"# Set to store customer orders (no duplicates allowed)\n",
"customer_orders = set()\n",
"\n",
"# Step a, b, c, and d: Add products to the customer order\n",
"while True:\n",
" product = input(\"Enter the name of a product that a customer wants to order: \").strip().lower()\n",
" # Add the product to the set of customer orders\n",
" if product in inventory: # Ensure the product is in the inventory\n",
" customer_orders.add(product)\n",
" print(f\"{product} has been added to your order.\")\n",
" else:\n",
" print(f\"Sorry, we don't have {product} in the inventory.\")\n",
"\n",
" # Ask if the customer wants to add another product\n",
" another_product = input(\"Do you want to add another product? (yes/no): \").strip().lower()\n",
" if another_product != 'yes':\n",
" break\n",
"\n",
"# Step to update the inventory based on the customer orders\n",
"for ordered_product in customer_orders:\n",
" if ordered_product in inventory and inventory[ordered_product] > 0:\n",
" inventory[ordered_product] -= 1\n",
" print(f\"One {ordered_product} has been deducted from the inventory.\")\n",
" else:\n",
" print(f\"Sorry, {ordered_product} is out of stock or not available.\")\n",
"\n",
"# Final inventory state\n",
"print(\"\\nUpdated inventory:\")\n",
"for product, quantity in inventory.items():\n",
" print(f\"{product.capitalize()}: {quantity}\")\n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "0d93d1b1-8f2f-4343-98b2-d43e2d0f640d",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the quantity of books available: 9\n",
"Enter the quantity of hats available: 15\n",
"Enter the quantity of t-shirts available: 5\n",
"Enter the quantity of mugs available: 3\n",
"Enter the quantity of keychains available: 8\n",
"Enter the product you want to order: keychain\n",
"Would you like to add another product? (yes/no): yes\n",
"Enter the product you want to order: hat\n",
"Would you like to add another product? (yes/no): no\n",
"Enter the price of keychain: 100\n",
"Enter the price of hat: 50\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Customer's Orders:\n",
"- Keychain\n",
"- Hat\n",
"\n",
"Total Price: $150.00\n",
"\n",
"Updated Inventory:\n",
"Book: 9\n",
"Hat: 14\n",
"T-shirt: 5\n",
"Mug: 3\n",
"Keychain: 7\n"
]
}
],
"source": [
"def initialize_inventory(products):\n",
" # Using dictionary comprehension to initialize inventory\n",
" return {product: int(input(f\"Enter the quantity of {product}s available: \")) for product in products}\n",
"\n",
"def get_customer_orders():\n",
" # Gather orders using a list comprehension: ask user for product names until 'no'\n",
" orders = []\n",
" while True:\n",
" product = input(\"Enter the product you want to order: \").lower()\n",
" orders.append(product) # Add the product to the orders list\n",
" another = input(\"Would you like to add another product? (yes/no): \").lower()\n",
" if another != \"yes\":\n",
" break\n",
" return orders\n",
"\n",
"def calculate_total_price(customer_orders):\n",
" # Using comprehension to get the price of each product and calculate the total price\n",
" return sum([float(input(f\"Enter the price of {product}: \")) for product in customer_orders])\n",
"\n",
"def update_inventory(inventory, customer_orders):\n",
" # Using dictionary comprehension to update inventory and filter out products with zero stock\n",
" for product in customer_orders:\n",
" if product in inventory and inventory[product] > 0:\n",
" inventory[product] -= 1\n",
" \n",
" # Filter products with zero stock from the inventory\n",
" inventory = {product: qty for product, qty in inventory.items() if qty > 0}\n",
" return inventory\n",
"\n",
"# List of products available in the store (can be predefined or extended by the user)\n",
"products = [\"book\", \"hat\", \"t-shirt\", \"mug\", \"keychain\"]\n",
"\n",
"# Initialize the inventory\n",
"inventory = initialize_inventory(products)\n",
"\n",
"# Get customer orders using comprehension\n",
"customer_orders = get_customer_orders()\n",
"\n",
"# Calculate total price of the customer order\n",
"total_price = calculate_total_price(customer_orders)\n",
"\n",
"# Update inventory after fulfilling the customer orders\n",
"inventory = update_inventory(inventory, customer_orders)\n",
"\n",
"# Display the customer orders and updated inventory\n",
"print(\"\\nCustomer's Orders:\")\n",
"for product in customer_orders:\n",
" print(f\"- {product.capitalize()}\")\n",
"\n",
"print(f\"\\nTotal Price: ${total_price:.2f}\")\n",
"\n",
"print(\"\\nUpdated Inventory:\")\n",
"for product, quantity in inventory.items():\n",
" print(f\"{product.capitalize()}: {quantity}\")\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b91103b8-0099-4d0f-a918-de2233322ed6",
"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.12.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading