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
219 changes: 218 additions & 1 deletion lab-python-flow-control.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,223 @@
"\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": "59aecd9e-d004-414b-b16f-c1d0e30ffb3f",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"how many t-shirt do you want to order: 1\n",
"how many mug do you want to order: 2\n",
"how many hat do you want to order: 3\n",
"how many book do you want to order: 4\n",
"how many keychain do you want to order: 5\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Print inventory: {'t-shirt': 1, 'mug': 2, 'hat': 3, 'book': 4, 'keychain': 5}\n"
]
}
],
"source": [
"# 0.Look at your code from the lab data structures, and improve repeated code with loops\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"inventory={}\n",
"\n",
"for product in products:\n",
" order_input = input(f\"how many {product} do you want to order: \")\n",
" if order_input.isdigit():\n",
" Order = int(order_input)\n",
" inventory[product] = Order\n",
" else:\n",
" inventory[product] = 0\n",
"print(\"Print inventory:\", (inventory))\n"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "3c4cc2ae-ca29-42be-9698-ebe8dbf75a7e",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Select {product} (or stop to close the inventory): keychain\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"'keychain' added to your order.\n",
"Remaining stock keychain: 0\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Select {product} (or stop to close the inventory): mug\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"'mug' added to your order.\n",
"Remaining stock mug: 0\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Select {product} (or stop to close the inventory): mug\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Sorry, 'mug' is sold out.\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Select {product} (or stop to close the inventory): mug\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Sorry, 'mug' is sold out.\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Select {product} (or stop to close the inventory): hat\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Sorry, 'hat' is sold out.\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Select {product} (or stop to close the inventory): hat\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Sorry, 'hat' is sold out.\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Select {product} (or stop to close the inventory): hat\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Sorry, 'hat' is sold out.\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Select {product} (or stop to close the inventory): book\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"'book' added to your order.\n",
"Remaining stock book: 2\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Select {product} (or stop to close the inventory): book\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"'book' added to your order.\n",
"Remaining stock book: 1\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Select {product} (or stop to close the inventory): stop\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"End of the order.\n",
"inventory up to date: {'t-shirt': 1, 'mug': 0, 'hat': 0, 'book': 1, 'keychain': 0}\n",
"costumer order up to date: {'keychain', 'book', 'mug'}\n"
]
}
],
"source": [
"customer_orders = set()\n",
"while True:\n",
" select = input(f\"Select {{product}} (or stop to close the inventory): \")\n",
" if select == \"stop\":\n",
" print(\"End of the order.\")\n",
" break\n",
" \n",
" if select in inventory and inventory[select] > 0: \n",
" customer_orders.add(select) \n",
" inventory[select] -= 1\n",
" print(f\"'{select}' added to your order.\")\n",
" print(f\"Remaining stock {select}: {inventory[select]}\")\n",
"\n",
" elif select in inventory and inventory[select] == 0:\n",
" print(f\"Sorry, '{select}' is sold out.\")\n",
" \n",
" elif select not in inventory:\n",
" print(f\"Error : This item '{select}' is not available in our inventory.\")\n",
"\n",
"unique\n",
"print(\"costumer order up to date: \", customer_orders)\n"
]
}
],
"metadata": {
Expand All @@ -55,7 +272,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.9"
}
},
"nbformat": 4,
Expand Down