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
188 changes: 185 additions & 3 deletions lab-python-flow-control.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,195 @@
"\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": 18,
"id": "af9c9d9e-0a84-46be-a9da-b640c3a35c19",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the qty t-shirt:, 20\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'t-shirt': 20}\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the qty mug:, 20\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'t-shirt': 20, 'mug': 20}\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the qty hat:, 20\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'t-shirt': 20, 'mug': 20, 'hat': 20}\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the qty book:, 20\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'t-shirt': 20, 'mug': 20, 'hat': 20, 'book': 20}\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the qty keychain:, 20\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'t-shirt': 20, 'mug': 20, 'hat': 20, 'book': 20, 'keychain': 20}\n"
]
}
],
"source": [
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"] #1 code with loops\n",
"inventory = {}\n",
"for product in products: \n",
" quantity = int(input(f\"Enter the qty {product}:, \"))\n",
" inventory[product] = quantity\n",
" print(inventory)\n",
"\n",
"customer_orders = set()"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "608e6078-9039-4972-a073-05f9cd1c9714",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The list of products ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter the item to order:, mug\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"mug added to your order\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Do you want to add another product yes/no?: no\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'mug'}\n"
]
}
],
"source": [
"while True: #2 customer order\n",
" print(\"The list of products\", products)\n",
" product = input(f\"Enter the item to order:, \").lower()\n",
" if product in products:\n",
" customer_orders.add(product)\n",
" print(f\"{product} added to your order\")\n",
" else:\n",
" print(\"Invalid product\")\n",
" choice= input(\"Do you want to add another product yes/no?: \")\n",
" if choice != \"yes\":\n",
" break\n",
"\n",
"print(customer_orders)"
]
},
{
"cell_type": "code",
"execution_count": 21,
"id": "5102183d-7e29-4ee6-8e56-c7e920937b2f",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Updated Inventory: {'t-shirt': 19, 'mug': 18, 'hat': 19, 'book': 19, 'keychain': 19}\n",
"t-shirt: 19\n",
"mug: 18\n",
"hat: 19\n",
"book: 19\n",
"keychain: 19\n"
]
}
],
"source": [
"for product in customer_orders: #3 updating inventory with customer order\n",
" if product in inventory and inventory[product] > 0:\n",
" inventory[product] -= 1\n",
"print(\"Updated Inventory:\", inventory)\n",
"for product, quantity in inventory.items():\n",
" print(f\"{product}: {quantity}\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e5ebd3ac-f445-45ee-a5e4-f1154fb6e1df",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python [conda env:base] *",
"language": "python",
"name": "python3"
"name": "conda-base-py"
},
"language_info": {
"codemirror_mode": {
Expand All @@ -55,7 +237,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.5"
}
},
"nbformat": 4,
Expand Down