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
82 changes: 79 additions & 3 deletions lab-python-flow-control.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,89 @@
"\n",
"3. Instead of updating the inventory by subtracting 1 from the quantity of each product, only do it for the products that were ordered (those in \"customer_orders\")."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "016a7163-78af-466f-9765-a0d060197191",
"metadata": {},
"outputs": [],
"source": [
"# 1. Define a list of products\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"\n",
"# 2. Create an empty dictionary for inventory\n",
"inventory = {}\n",
"\n",
"# 3. Ask the user for quantity of each product\n",
"print(\"Enter the quantity available for each product:\")\n",
"for product in products:\n",
" quantity = input(f\"Quantity of {product}: \")\n",
"\n",
" # Ensure user enters a valid number\n",
" while not quantity.isdigit():\n",
" print(\"Please enter a valid number.\")\n",
" quantity = input(f\"Quantity of {product}: \")\n",
"\n",
" inventory[product] = int(quantity)\n",
"\n",
"\n",
"# 4. Create an empty set for customer orders\n",
"customer_orders = set()\n",
"\n",
"# 5. Ask the user to enter product orders repeatedly\n",
"print(\"\\nEnter the products that the customer wants to order.\")\n",
"while True:\n",
" order = input(\"Enter a product name: \").lower()\n",
"\n",
" # Validate input\n",
" while order not in products:\n",
" print(\"Invalid product. Please enter a valid product from the list.\")\n",
" order = input(\"Enter a product name: \").lower()\n",
"\n",
" customer_orders.add(order)\n",
"\n",
" # Ask if they want to add another product\n",
" another = input(\"Add another product? (yes/no): \").lower()\n",
" if another != \"yes\":\n",
" break\n",
"\n",
"\n",
"# 6. Print the products ordered\n",
"print(\"\\nCustomer has ordered these products:\")\n",
"for item in customer_orders:\n",
" print(\"-\", item)\n",
"\n",
"\n",
"# 7. Calculate statistics\n",
"total_products_ordered = len(customer_orders)\n",
"percentage_ordered = (total_products_ordered / len(products)) * 100\n",
"\n",
"# 8. Store statistics in a tuple\n",
"order_status = (total_products_ordered, percentage_ordered)\n",
"\n",
"# 9. Print order statistics\n",
"print(\"\\nOrder Statistics:\")\n",
"print(f\"Total Products Ordered: {order_status[0]}\")\n",
"print(f\"Percentage of Products Ordered: {order_status[1]:.2f}%\")\n",
"\n",
"\n",
"# 10. Update inventory for only ordered products\n",
"for item in customer_orders:\n",
" inventory[item] -= 1\n",
"\n",
"# 11. Print updated inventory\n",
"print(\"\\nUpdated Inventory:\")\n",
"for product, qty in inventory.items():\n",
" print(f\"{product}: {qty}\")"
]
}
],
"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 @@ -55,7 +131,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.5"
}
},
"nbformat": 4,
Expand Down