Skip to content

lab solved #459

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
136 changes: 135 additions & 1 deletion lab-python-flow-control.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,140 @@
"\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": null,
"id": "9b3265b3-bb16-4fec-a80b-0655aa4a903f",
"metadata": {},
"outputs": [],
"source": [
"# 1. \n",
"#customer_orders = set()\n",
"\n",
"#inventory = {}\n",
"\n",
"#for item in customer_orders:\n",
" #if item in inventory and inventory[item] > 0:\n",
" inventory[item] -= 1\n",
"#print(\"Updated Inventory:\")\n",
"#for product, quantity in inventory.items():\n",
" # print(f\"{product}: {quantity}\")"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "d40fb142-1389-4007-97cb-2bac8f76162b",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Available products: t-shirt, mug, hat, book, keychain\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter at least one product to order: t-shirt\n"
]
}
],
"source": [
"# 2.a & .b & .c.\n",
"inventory = {\n",
" \"t-shirt\": 10,\n",
" \"mug\": 10,\n",
" \"hat\": 10,\n",
" \"book\": 10,\n",
" \"keychain\": 10\n",
"}\n",
"\n",
"customer_orders = []\n",
"print(\"Available products:\", \", \".join(inventory.keys()))\n",
"while True:\n",
" first_order = input(\"Enter at least one product to order: \").strip().lower()\n",
" if first_order in inventory:\n",
" customer_orders.append(first_order)\n",
" break\n",
" else:\n",
" print(\"Invalid product. Please choose from the available products.\")"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "f9fde0ec-cf6e-41ea-b020-1152c1eaf9c8",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Do you want to add more products? (yes/no): yes\n",
"Enter more product names separated by commas: mug\n",
"Do you want to add more products? (yes/no): no\n"
]
}
],
"source": [
"# 2.d.\n",
"while True:\n",
" more = input(\"Do you want to add more products? (yes/no): \").strip().lower()\n",
" if more != \"yes\":\n",
" break\n",
"\n",
" additional_input = input(\"Enter more product names separated by commas: \")\n",
" product_list = [p.strip().lower() for p in additional_input.split(\",\")]\n",
"\n",
" for product in product_list:\n",
" if product in inventory:\n",
" customer_orders.append(product)\n",
" else:\n",
" print(f\"'{product}' is not a valid product. Skipping.\")"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "632be76b-10ba-4bc7-8b2f-612bfeb6b25d",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Updated Inventory:\n",
"t-shirt: 9\n",
"mug: 9\n",
"hat: 10\n",
"book: 10\n",
"keychain: 10\n"
]
}
],
"source": [
"# 3.\n",
"for item in customer_orders:\n",
" if item in inventory and inventory[item] > 0:\n",
" inventory[item] -= 1\n",
"\n",
"print(\"\\nUpdated Inventory:\")\n",
"for product, quantity in inventory.items():\n",
" print(f\"{product}: {quantity}\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "dc74dbb3-a7eb-4959-a2cd-474fc0ef32c6",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand All @@ -55,7 +189,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.12.5"
}
},
"nbformat": 4,
Expand Down