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
106 changes: 104 additions & 2 deletions lab-python-flow-control.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,113 @@
"\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": 1,
"id": "0f0dbd31",
"metadata": {},
"outputs": [],
"source": [
"\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"\n",
"customer_orders = set()\n",
"\n",
"inventory = {\"t-shirt\": 10, \"mug\" : 5, \"hat\": 2, \"book\": 0, \"keychain\": 15}\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cc6c750a",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Available products: ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n",
"Added hat to your order.\n",
"Available products: ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n",
"Added hat to your order.\n",
"Available products: ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n",
"Added book to your order.\n",
"Your final order is: {'hat', 'book'} Thank you for shopping with us!\n",
"{customer_order} is available. Remaining stock: {inventory[customer_order]}\n",
"Sorry, book is out of stock.\n"
]
}
],
"source": [
"while True:\n",
" print(\"Available products:\", products)\n",
" product = input(\"Enter the product you want to order:\")\n",
" \n",
"\n",
" if product in products:\n",
" customer_orders.add(product) \n",
" print(f\"Added {product} to your order.\")\n",
"\n",
" else:\n",
" print(\"Sorry, we don't have that product.\") \n",
" \n",
" continue_ordering = input(\"Do you want to order another product? (yes/no): \")\n",
" if continue_ordering.lower() != \"yes\":\n",
" break\n",
" \n",
"print(\"Your final order is:\", customer_orders, \"Thank you for shopping with us!\")\n",
"\n",
"for customer_order in customer_orders:\n",
" if inventory[customer_order] > 0:\n",
" inventory[customer_order] -= 1\n",
" print(\"{customer_order} is available. Remaining stock: {inventory[customer_order]}\")\n",
" else:\n",
" print(f\"Sorry, {customer_order} is out of stock.\")\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "019781a1",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 3,
"id": "f0f580e5",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'t-shirt': 10, 'mug': 5, 'hat': 1, 'book': 0, 'keychain': 15}"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"inventory"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "409fcb47",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -55,7 +157,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.5"
}
},
"nbformat": 4,
Expand Down