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
77 changes: 75 additions & 2 deletions lab-python-flow-control.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,84 @@
"\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": 2,
"id": "680787d1",
"metadata": {},
"outputs": [],
"source": [
"# Step 1: Define inventory and customer_orders\n",
"inventory = {\n",
" 'apple': 10,\n",
" 'banana': 8,\n",
" 'orange': 15,\n",
" 'grape': 5\n",
"}\n",
"customer_orders = set()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "ab9b918e",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"orange added to the order.\n",
"Customer orders: {'orange'}\n"
]
}
],
"source": [
"# Step 2: Collect customer orders using a loop\n",
"while True:\n",
" product = input(\"Enter the name of a product to order: \").strip().lower()\n",
" if product in inventory:\n",
" customer_orders.add(product)\n",
" print(f\"{product} added to the order.\")\n",
" else:\n",
" print(\"Product not found in inventory.\")\n",
" another = input(\"Do you want to add another product? (yes/no): \").strip().lower()\n",
" if another != 'yes':\n",
" break\n",
"print(\"Customer orders:\", customer_orders)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "d9f8254a",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Inventory updated: orange now has 14 left.\n",
"Updated inventory: {'apple': 10, 'banana': 8, 'orange': 14, 'grape': 5}\n"
]
}
],
"source": [
"# Step 3: Update inventory for ordered products only\n",
"for product in customer_orders:\n",
" if inventory[product] > 0:\n",
" inventory[product] -= 1\n",
" print(f\"Inventory updated: {product} now has {inventory[product]} left.\")\n",
" else:\n",
" print(f\"{product} is out of stock!\")\n",
"print(\"Updated inventory:\", inventory)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -55,7 +128,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.5"
}
},
"nbformat": 4,
Expand Down