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
88 changes: 88 additions & 0 deletions lab-python-functions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,94 @@
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d0cdfefa",
"metadata": {},
"outputs": [],
"source": [
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"\n",
"# 1.\n",
"def initialize_inventory(products):\n",
" inventory = {}\n",
" for product in products:\n",
" quantity = int(input(f\"Enter the quantity of {product} available in inventory: \"))\n",
" inventory[product] = quantity\n",
" return inventory\n",
"\n",
"# 2.\n",
"def get_customer_orders():\n",
" customer_orders = set()\n",
" while True:\n",
" order = input(\"\\nEnter the name of a product to order (t-shirt, mug, hat, book, keychain): \").strip().lower()\n",
" \n",
" if order in products:\n",
" customer_orders.add(order)\n",
" print(f\"'{order}' has been added to your order.\")\n",
" else:\n",
" print(f\"Invalid product. Please choose from: {', '.join(products)}\")\n",
" continue\n",
"\n",
" another = input(\"Do you want to add another product? (yes/no): \").strip().lower()\n",
" if another != \"yes\":\n",
" break\n",
"\n",
" return customer_orders\n",
"\n",
"# 3.\n",
"def update_inventory(customer_orders, inventory):\n",
" for product in customer_orders:\n",
" if product in inventory:\n",
" inventory[product] -= 1\n",
" return inventory\n",
"\n",
"# 4.\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",
" order_statistics = (total_products_ordered, percentage_ordered)\n",
" return order_statistics\n",
"\n",
"# 5.\n",
"def print_order_statistics(order_statistics):\n",
" print(f\"\"\"\n",
"Order Statistics:\n",
"Total Products Ordered: {order_statistics[0]}\n",
"Percentage of Products Ordered: {order_statistics[1]:.2f}%\n",
" \"\"\")\n",
"\n",
"# 6.\n",
"def print_updated_inventory(inventory):\n",
" print(\"Updated Inventory:\")\n",
" for product, quantity in inventory.items():\n",
" print(f\"{product}: {quantity}\")\n",
"\n",
"# 7.\n",
"# Step 1: Initialize the inventory\n",
"inventory = initialize_inventory(products)\n",
"\n",
"# Step 2: Get customer orders\n",
"customer_orders = get_customer_orders()\n",
"\n",
"# Step 3: Calculate order statistics\n",
"order_statistics = calculate_order_statistics(customer_orders, products)\n",
"\n",
"# Step 4: Print order statistics\n",
"print_order_statistics(order_statistics)\n",
"\n",
"# Step 5: Update the inventory\n",
"inventory = update_inventory(customer_orders, inventory)\n",
"\n",
"# Step 6: Print the updated inventory\n",
"print_updated_inventory(inventory)\n",
"\n",
"# Step 7: Calculate and print total price\n",
"total_price = calculate_total_price(customer_orders)\n",
"print(f\"Total Price: {total_price}\")"
]
}
],
"metadata": {
Expand Down