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

Completed Labs #355

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
297 changes: 297 additions & 0 deletions .ipynb_checkpoints/lab-python-list-comprehension-checkpoint.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,297 @@
{
"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": 78,
"id": "30f3bb8b-4519-4c25-b8a2-66e35a556dff",
"metadata": {},
"outputs": [],
"source": [
"# Step 1: Initialize inventory using comprehension\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"
]
},
{
"cell_type": "code",
"execution_count": 79,
"id": "a8b81286-942c-4e16-9d8a-eceeebba5525",
"metadata": {},
"outputs": [],
"source": [
"# Step 2: Get customer orders using comprehension\n",
"def get_customer_orders():\n",
" products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
" num_orders = int(input(\"Enter the number of customer orders: \"))\n",
" customer_orders = {input(f\"Enter the name of product: \") for i in range(num_orders)} \n",
" return customer_orders"
]
},
{
"cell_type": "code",
"execution_count": 80,
"id": "487083ab-5ca8-4ec3-b501-9fd89a396023",
"metadata": {},
"outputs": [],
"source": [
"# Step 3: Calculate total price of customer orders\n",
"def calculate_total_price(customer_orders):\n",
" total_price = sum(float(input(f\"Enter the price of {product}: \")) for product in customer_orders)\n",
" return total_price"
]
},
{
"cell_type": "code",
"execution_count": 81,
"id": "47e6f777-2f47-467d-953c-823a14810122",
"metadata": {},
"outputs": [],
"source": [
"# Step 4: Update inventory and remove products with zero quantity\n",
"def update_inventory(customer_orders, inventory):\n",
" inventory = {product: inventory[product] - 1 for product in customer_orders if product in inventory and inventory[product] > 0}\n",
" inventory = {product: quantity for product, quantity in inventory.items() if quantity > 0} # Remove products with zero quantity\n",
" return inventory"
]
},
{
"cell_type": "code",
"execution_count": 82,
"id": "49087b5f-59cc-45ce-bc4a-9bfebf07ae81",
"metadata": {},
"outputs": [],
"source": [
"# Step 5: Calculate 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"
]
},
{
"cell_type": "code",
"execution_count": 83,
"id": "b547e64c-f54b-44f9-a6c4-b64659270f1c",
"metadata": {},
"outputs": [],
"source": [
"# Step 6: Print order statistics\n",
"def print_order_statistics(order_statistics):\n",
" print(\"Order Statistics:\")\n",
" print(\"Total Products Ordered:\", order_statistics[0])\n",
" print(\"Percentage of Unique Products Ordered:\", order_statistics[1])"
]
},
{
"cell_type": "code",
"execution_count": 84,
"id": "c2f016ab-866d-468e-951b-e0ae67d3221c",
"metadata": {},
"outputs": [],
"source": [
"# Step 7: Print updated inventory\n",
"def print_updated_inventory(inventory):\n",
" print(\"Updated Inventory:\")\n",
" for product, quantity in inventory.items():\n",
" print(f\"{product}: {quantity}\")"
]
},
{
"cell_type": "code",
"execution_count": 85,
"id": "bd50bce8-bddc-44c9-a10c-1d267e05b6d3",
"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: 3\n",
"Enter the quantity of books available: 2\n",
"Enter the quantity of keychains available: 1\n",
"Enter the number of customer orders: 4\n",
"Enter the name of product: mug\n",
"Enter the name of product: book\n",
"Enter the name of product: hat\n",
"Enter the name of product: keychain\n",
"Enter the price of mug: 5\n",
"Enter the price of hat: 10\n",
"Enter the price of book: 3\n",
"Enter the price of keychain: 2\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Order Statistics:\n",
"Total Products Ordered: 4\n",
"Percentage of Unique Products Ordered: 80.0\n",
"Updated Inventory:\n",
"mug: 3\n",
"hat: 2\n",
"book: 1\n",
"Total Price: 20.00\n"
]
}
],
"source": [
"# Step 8: Main function\n",
"def main():\n",
" products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
" inventory = initialize_inventory(products)\n",
" customer_orders = get_customer_orders()\n",
" total_price = calculate_total_price(customer_orders)\n",
" inventory = update_inventory(customer_orders, inventory)\n",
" order_statistics = calculate_order_statistics(customer_orders, products)\n",
" print_order_statistics(order_statistics)\n",
" print_updated_inventory(inventory)\n",
" print(f\"Total Price: {total_price:.2f}\")\n",
"\n",
"\n",
"# Execute the program\n",
"main()"
]
},
{
"cell_type": "markdown",
"id": "0f12fbd8-8dcb-4594-b6e2-cd31a2f9fd86",
"metadata": {},
"source": [
"##### "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b300f6ae-95d7-4a3a-a1d1-ee3f31bbdc87",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "a60206cd-7639-4834-b1f5-214f089d6173",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "153b2379-3fd9-47ae-84bd-6ecc30e53b09",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "5f6e2753-7c4d-42a6-b56e-1c259c90fc35",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python [conda env:base] *",
"language": "python",
"name": "conda-base-py"
},
"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