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
130 changes: 127 additions & 3 deletions lab-python-flow-control.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,137 @@
"\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": 1,
"id": "c3774736-027e-4e27-93e5-8edb7f190286",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Enter the quantity available for each product:\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"t-shirt: 5\n",
"mug: 5\n",
"hat: 5\n",
"book: 5\n",
"keychain: 20\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Available products: ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter a product to order: mug\n",
"Do you want to add another product? (yes/no): no\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Products ordered by the customer:\n",
"mug\n",
"\n",
"Order Statistics:\n",
"Total Products Ordered: 1\n",
"Percentage of Products Ordered: 20.00%\n",
"\n",
"Updated Inventory:\n",
"t-shirt: 5\n",
"mug: 4\n",
"hat: 5\n",
"book: 5\n",
"keychain: 20\n"
]
}
],
"source": [
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"inventory = {}\n",
"\n",
"#1 ask the user for items\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",
"#2 customer orders (loop)\n",
"customer_orders = set()\n",
"\n",
"print(\"\\nAvailable products:\", products)\n",
"\n",
"while True:\n",
" product_choice = input(\"Enter a product to order: \").lower()\n",
"\n",
" if product_choice in products:\n",
" customer_orders.add(product_choice)\n",
" else:\n",
" print(\"Invalid product, please choose from the list.\")\n",
"\n",
" another = input(\"Do you want to add another product? (yes/no): \").lower()\n",
" if another != \"yes\":\n",
" break\n",
"\n",
"#3 products ordered\n",
"print(\"\\nProducts ordered by the customer:\")\n",
"for order in customer_orders:\n",
" print(order)\n",
"\n",
"#4 calculate order statistics\n",
"total_products_ordered = len(customer_orders) \n",
"total_products_available = len(products) \n",
"percentage_ordered = (total_products_ordered / total_products_available) * 100\n",
"\n",
"# store tuple\n",
"order_status = (total_products_ordered, percentage_ordered)\n",
"\n",
"#5 print results\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",
"#6 update inventory only for ordered products\n",
"for product in customer_orders:\n",
" inventory[product] -= 1\n",
"\n",
"#7 updated inventory\n",
"print(\"\\nUpdated Inventory:\")\n",
"for product in inventory:\n",
" print(f\"{product}: {inventory[product]}\")\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "837b27b5-1fa2-4958-acdb-228bb5ed80d0",
"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 @@ -55,7 +179,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.5"
}
},
"nbformat": 4,
Expand Down