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
150 changes: 148 additions & 2 deletions lab-python-flow-control.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,152 @@
"# Lab | Flow Control"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "2c8fb83a",
"metadata": {},
"outputs": [],
"source": [
"#products list\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "08e55667",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"quantity available of each product:\n",
" please enter a non-negative number.\n"
]
}
],
"source": [
"#inventory with user input\n",
"inventory = {}\n",
"print(\"quantity available of each product:\")\n",
"for item in products:\n",
" while True:\n",
" quantity = input(f\" {item}: \").strip()\n",
" if quantity.isdigit(): \n",
" inventory[item] = int(quantity)\n",
" break\n",
" print(\" please enter a non-negative number.\")"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "a363df15",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Add products to the order. Available: t-shirt, mug, hat, book, keychain\n"
]
}
],
"source": [
"# customer orders\n",
"orders = set() \n",
"print(\"Add products to the order. Available:\", \", \".join(products))\n",
"\n",
"while True:\n",
" article = input(\"the product name: \").strip().lower()\n",
" if article in products:\n",
" orders.add(article)\n",
" else:\n",
" print(\"product not available, please select from the list.\")\n",
" break\n",
"\n",
" more = input(\"do you want to add another product? (yes/no): \").strip().lower()\n",
" if more in (\"no\"):\n",
" break"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8840513f",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"customer ordered: hat, mug\n"
]
}
],
"source": [
"# show customer order\n",
"print(\"customer ordered:\", \", \".join(sorted(orders)) or \"(none)\")"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "bff78a4c",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"order details:\n",
"total products ordered: 2\n",
"percentage of products ordered: 40.0%\n"
]
}
],
"source": [
"# order details\n",
"total_products_ordered = len(orders)\n",
"percentage_ordered = round((total_products_ordered / len(products)) * 100, 2)\n",
"order_status = (total_products_ordered, percentage_ordered)\n",
"\n",
"print(\"order details:\")\n",
"print(f\"total products ordered: {order_status[0]}\")\n",
"print(f\"percentage of products ordered: {order_status[1]}%\")"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "78a9642e",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"updated inventory after order:\n",
" t-shirt: 1\n",
" mug: 1\n",
" hat: 2\n",
" book: 4\n",
" keychain: 5\n"
]
}
],
"source": [
"# update and print inventory after order\n",
"for item in orders:\n",
" if inventory.get(item, 0) > 0:\n",
" inventory[item] -= 1\n",
"print(\"updated inventory after order:\")\n",
"for item in products:\n",
" print(f\" {item}: {inventory.get(item, 0)}\")"
]
},
{
"cell_type": "markdown",
"id": "3851fcd1-cf98-4653-9c89-e003b7ec9400",
Expand Down Expand Up @@ -41,7 +187,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -55,7 +201,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.5"
}
},
"nbformat": 4,
Expand Down