Skip to content

Completed Exercise - Managing Customer Orders #520

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

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
126 changes: 123 additions & 3 deletions lab-python-data-structures.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,133 @@
"\n",
"Solve the exercise by implementing the steps using the Python concepts of lists, dictionaries, sets, and basic input/output operations. "
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Enter the quantity available for each product:\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"t-shirt: 10\n",
"mug: 5\n",
"hat: 3\n",
"book: 7\n",
"keychain: 4\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Enter 3 products the customer wants to order (choose from t-shirt, mug, hat, book, keychain):\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Product 1: mug\n",
"Product 2: hat\n",
"Product 3: book\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Products in customer order:\n",
"- book\n",
"- mug\n",
"- hat\n",
"\n",
"Order Statistics:\n",
"Total Products Ordered: 3\n",
"Percentage of Products Ordered: 60.00%\n",
"\n",
"Updated Inventory:\n",
"t-shirt: 10\n",
"mug: 4\n",
"hat: 2\n",
"book: 6\n",
"keychain: 4\n"
]
}
],
"source": [
"# Step 1: Define product list\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"\n",
"# Step 2: Create empty inventory dictionary\n",
"inventory = {}\n",
"\n",
"# Step 3: Ask user to input quantity for each product\n",
"print(\"Enter the quantity available for each product:\")\n",
"for product in products:\n",
" quantity = int(input(f\"{product}: \"))\n",
" inventory[product] = quantity\n",
"\n",
"# Step 4: Create empty set for customer orders\n",
"customer_orders = set()\n",
"\n",
"# Step 5: Get 3 product orders from customer\n",
"print(\"\\nEnter 3 products the customer wants to order (choose from t-shirt, mug, hat, book, keychain):\")\n",
"while len(customer_orders) < 3:\n",
" order = input(f\"Product {len(customer_orders)+1}: \").strip().lower()\n",
" if order in products:\n",
" customer_orders.add(order)\n",
" else:\n",
" print(\"Invalid product. Please choose from the available products.\")\n",
"\n",
"# Step 6: Print ordered products\n",
"print(\"\\nProducts in customer order:\")\n",
"for item in customer_orders:\n",
" print(\"-\", item)\n",
"\n",
"# Step 7: Calculate order statistics\n",
"total_products_ordered = len(customer_orders)\n",
"percentage_ordered = (total_products_ordered / len(products)) * 100\n",
"order_status = (total_products_ordered, percentage_ordered)\n",
"\n",
"# Step 8: Print order statistics\n",
"print(\"\\nOrder Statistics:\")\n",
"print(\"Total Products Ordered:\", order_status[0])\n",
"print(f\"Percentage of Products Ordered: {order_status[1]:.2f}%\")\n",
"\n",
"# Step 9: Update inventory (subtract 1 for each ordered product)\n",
"for item in customer_orders:\n",
" inventory[item] -= 1 # assume customer ordered one unit per product\n",
"\n",
"# Step 10: Print updated inventory\n",
"print(\"\\nUpdated Inventory:\")\n",
"for product, qty in inventory.items():\n",
" print(f\"{product}: {qty}\")\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python [conda env:base] *",
"language": "python",
"name": "python3"
"name": "conda-base-py"
},
"language_info": {
"codemirror_mode": {
Expand All @@ -68,7 +188,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.12.7"
}
},
"nbformat": 4,
Expand Down