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
171 changes: 169 additions & 2 deletions lab-python-flow-control.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,173 @@
"# Lab | Flow Control"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "8619afc7",
"metadata": {},
"outputs": [],
"source": [
"#1 Define the product list\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "3adb391a",
"metadata": {},
"outputs": [],
"source": [
"#2 Create an empty inventory dictionary\n",
"inventory = {}"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "3dad3e35",
"metadata": {},
"outputs": [],
"source": [
"#3 Input quantity for each product\n",
"for product in products:\n",
" quantity = int(input(f\"Enter quantity available for {product}: \"))\n",
" inventory[product] = quantity"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "48acd4f7",
"metadata": {},
"outputs": [],
"source": [
" #4 Create an empty set for customer orders\n",
"customer_orders = set()"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "a49c9c8a",
"metadata": {},
"outputs": [],
"source": [
"#5 Ask for product orders (fixed maximum number of attempts, e.g., 10)\n",
"for _ in range(10): # You can change 10 to a reasonable number\n",
" order = input(\"Enter a product to order (t-shirt, mug, hat, book, keychain): \").strip().lower()\n",
"\n",
" if order in products:\n",
" customer_orders.add(order)\n",
" else:\n",
" print(\"Invalid product. Try again.\")\n",
" continue\n",
"\n",
" add_more = input(\"Do you want to add another product? (yes/no): \").strip().lower()\n",
" if add_more != \"yes\":\n",
" break"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "dda38058",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Customer Orders: {'book', 'mug', 'hat'}\n"
]
}
],
"source": [
"#6 Print the customer orders\n",
"print(\"\\nCustomer Orders:\", customer_orders)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "b31ea90f",
"metadata": {},
"outputs": [],
"source": [
"#7 Calculate 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)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "4723460e",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Order Statistics:\n",
"Total Products Ordered: 3\n",
"Percentage of Products Ordered: 60.00%\n"
]
}
],
"source": [
"#8 Print 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}%\")"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "0fecd67c",
"metadata": {},
"outputs": [],
"source": [
"#9 Update inventory only for ordered items\n",
"for product in customer_orders:\n",
" if inventory[product] > 0:\n",
" inventory[product] -= 1\n",
" else:\n",
" print(f\"Warning: {product} is out of stock!\")"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "c5dae8f3",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Updated Inventory:\n",
"t-shirt: 9\n",
"mug: 7\n",
"hat: 6\n",
"book: 5\n",
"keychain: 5\n"
]
}
],
"source": [
"#10 Print updated inventory\n",
"print(\"\\nUpdated Inventory:\")\n",
"for product in inventory:\n",
" print(f\"{product}: {inventory[product]}\")"
]
},
{
"cell_type": "markdown",
"id": "3851fcd1-cf98-4653-9c89-e003b7ec9400",
Expand Down Expand Up @@ -41,7 +208,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -55,7 +222,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.5"
}
},
"nbformat": 4,
Expand Down