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

lab4 list-dict #347

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
111 changes: 109 additions & 2 deletions lab-python-list-comprehension.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,118 @@
"\n",
"```\n"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "ed255fe4",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Order Statistics:\n",
"Total Products Ordered: 1\n",
"Percentage of Unique Products Ordered: 20.0\n",
"\n",
"Updated Inventory:\n",
"t-shirt: 3\n",
"mug: 4\n",
"hat: 75\n",
"book: 33\n",
"keychain: 11\n",
"\n",
"Total Price: 2.00\n"
]
}
],
"source": [
"def initialize_inventory(products):\n",
" \"\"\"\n",
" Initialize the inventory with product names and quantities.\n",
" Use comprehension to create the inventory dictionary.\n",
" \"\"\"\n",
" inventory = {product: int(input(f\"Enter the quantity of {product}s available: \")) for product in products}\n",
" return inventory\n",
"\n",
"\n",
"def get_customer_orders():\n",
" \"\"\"\n",
" Gather customer orders using a loop and comprehension.\n",
" \"\"\"\n",
" num_orders = int(input(\"Enter the number of customer orders: \"))\n",
" customer_orders = {input(f\"Enter the name of a product that a customer wants to order: \").strip() for _ in range(num_orders)}\n",
" return customer_orders\n",
"\n",
"\n",
"def calculate_total_price(customer_orders):\n",
" \"\"\"\n",
" Calculate the total price of the customer's order.\n",
" Prompt the user to enter the price for each product in the customer order.\n",
" Use comprehension to calculate the total.\n",
" \"\"\"\n",
" total_price = sum(float(input(f\"Enter the price of {product}: \")) for product in customer_orders)\n",
" return total_price\n",
"\n",
"\n",
"def update_inventory(customer_orders, inventory):\n",
" \"\"\"\n",
" Update the inventory based on the customer orders.\n",
" Use comprehension to decrease the quantity and remove products with zero quantity.\n",
" \"\"\"\n",
" inventory = {product: quantity - 1 for product, quantity in inventory.items() if product not in customer_orders or quantity - 1 > 0}\n",
" return inventory\n",
"\n",
"\n",
"def print_updated_inventory(inventory):\n",
" \"\"\"\n",
" Print the updated inventory.\n",
" \"\"\"\n",
" print(\"\\nUpdated Inventory:\")\n",
" for product, quantity in inventory.items():\n",
" print(f\"{product}: {quantity}\")\n",
"\n",
"\n",
"def main():\n",
" # Step 1: Define products\n",
" products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"\n",
" # Step 2: Initialize inventory\n",
" inventory = initialize_inventory(products)\n",
"\n",
" # Step 3: Get customer orders\n",
" customer_orders = get_customer_orders()\n",
"\n",
" # Step 4: Calculate total price of the customer order\n",
" total_price = calculate_total_price(customer_orders)\n",
"\n",
" # Step 5: Update inventory\n",
" inventory = update_inventory(customer_orders, inventory)\n",
"\n",
" # Step 6: Print order statistics\n",
" total_products_ordered = len(customer_orders)\n",
" percentage_unique_ordered = (total_products_ordered / len(products)) * 100\n",
" print(\"\\nOrder Statistics:\")\n",
" print(f\"Total Products Ordered: {total_products_ordered}\")\n",
" print(f\"Percentage of Unique Products Ordered: {percentage_unique_ordered:.1f}\")\n",
"\n",
" # Step 7: Print updated inventory\n",
" print_updated_inventory(inventory)\n",
"\n",
" # Step 8: Print total price\n",
" print(f\"\\nTotal Price: {total_price:.2f}\")\n",
"\n",
"\n",
"# Run the program\n",
"main()\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -93,7 +200,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.12.7"
}
},
"nbformat": 4,
Expand Down