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
212 changes: 208 additions & 4 deletions lab-python-flow-control.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
},
{
"cell_type": "markdown",
"id": "3851fcd1-cf98-4653-9c89-e003b7ec9400",
"id": "df0eaec9-6d59-4f06-98d3-bb640aa365db",
"metadata": {},
"source": [
"## Exercise: Managing Customer Orders Optimized\n",
Expand All @@ -37,13 +37,217 @@
"\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": "fa3c96cd-834c-45eb-b368-99446a287c15",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the quantity of t-shirt: 5\n",
"Enter the quantity of mug: 4\n",
"Enter the quantity of hat: 3\n",
"Enter the quantity of book: 5\n",
"Enter the quantity of keychain: 2\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Inventory: {'t-shirt': 5, 'mug': 4, 'hat': 3, 'book': 5, 'keychain': 2}\n"
]
}
],
"source": [
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"\n",
"inventory = {}\n",
"\n",
"for product in products:\n",
" quantity = int(input(f\"Enter the quantity of {product}: \"))\n",
" inventory[product] = quantity\n",
"\n",
"print(\"Inventory:\", inventory)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "5c374a99-7658-40d3-ba62-de733d261ff3",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the name of a product you want to order: book\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"book was added to your order.\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Would you like to add another product? (yes/no): yes\n",
"Enter the name of a product you want to order: keychain\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"keychain was added to your order.\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Would you like to add another product? (yes/no): yes\n",
"Enter the name of a product you want to order: hat\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"hat was added to your order.\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Would you like to add another product? (yes/no): yes\n",
"Enter the name of a product you want to order: mug\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"mug was added to your order.\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Would you like to add another product? (yes/no): yes\n",
"Enter the name of a product you want to order: nail\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"nail is not available.\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Would you like to add another product? (yes/no): yes\n",
"Enter the name of a product you want to order: t-shirt\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"t-shirt was added to your order.\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Would you like to add another product? (yes/no): no\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Customer orders: {'keychain', 'mug', 't-shirt', 'hat', 'book'}\n"
]
}
],
"source": [
"\n",
"customer_orders = set()\n",
"\n",
"while True:\n",
" product = input(\n",
" \"Enter the name of a product you want to order: \"\n",
" ).strip().lower()\n",
"\n",
" if product in inventory:\n",
" customer_orders.add(product)\n",
" print(f\"{product} was added to your order.\")\n",
" else:\n",
" print(f\"{product} is not available.\")\n",
"\n",
" another_product = input(\n",
" \"Would you like to add another product? (yes/no): \"\n",
" ).strip().lower()\n",
"\n",
" if another_product != \"yes\":\n",
" break\n",
"\n",
"print(\"Customer orders:\", customer_orders)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "01d83dd6-8153-49fd-9560-bd2e977b5349",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Updated inventory: {'t-shirt': 4, 'mug': 3, 'hat': 2, 'book': 4, 'keychain': 1}\n"
]
}
],
"source": [
"for product in customer_orders:\n",
" if inventory[product] > 0:\n",
" inventory[product] -= 1\n",
" else:\n",
" print(f\"{product} is out of stock.\")\n",
"\n",
"print(\"Updated inventory:\", inventory)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1df43a23-445b-4e23-9b2b-2975f63fee20",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "pj1_env",
"language": "python",
"name": "python3"
"name": "venv"
},
"language_info": {
"codemirror_mode": {
Expand All @@ -55,7 +259,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.12.13"
}
},
"nbformat": 4,
Expand Down