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
159 changes: 156 additions & 3 deletions lab-python-flow-control.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,166 @@
"\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": "28796da9-daf3-43b3-84e7-e8932bb841f4",
"metadata": {},
"outputs": [],
"source": [
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"\n",
"inventory = {}\n",
"\n",
"for product in products:\n",
" quantity = int(input(f\"Enter quantity for {product}: \"))\n",
" inventory[product] = quantity\n",
"\n",
"customer_orders = set()\n",
"\n",
"while len(customer_orders) < 3:\n",
" user_input = input(\"Enter a product to order: \")\n",
"\n",
" if user_input not in products:\n",
" print(\"Invalid product. Try again.\")\n",
" continue\n",
"\n",
" if user_input in customer_orders:\n",
" print(\"Product already chosen. Pick another one.\")\n",
" continue\n",
"\n",
" customer_orders.add(user_input)\n",
"\n",
"print(f\"\\nOrder received: {customer_orders}\")\n",
"\n",
"total_ordered = len(customer_orders)\n",
"total_products = len(inventory)\n",
"percentage = (total_ordered / total_products) * 100 if total_products > 0 else 0\n",
"\n",
"order_status = (total_ordered, percentage)\n",
"\n",
"print(f\"Order Statistics: \")\n",
"print(f\"Total distinct products ordered: {total_ordered}\")\n",
"print(f\"Percentage vs inventory: {percentage}%\")\n",
"\n",
"for product in customer_orders:\n",
" if inventory[product] > 0:\n",
" inventory[product] -= 1\n",
" print(f\"{product} processed. New stock: {inventory[product]}\")\n",
" else:\n",
" print(f\"{product} is OUT OF STOCK.\")\n",
"\n",
"for product, quantity in inventory.items():\n",
" print(f\"{product}: {quantity}\")"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "85ac97e8-f0a1-41e2-a110-921dd27eb454",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter a product: book\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Added book to the 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": [
"Customer order: {'book'}\n"
]
}
],
"source": [
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"customer_orders = set()\n",
"\n",
"while True:\n",
" product = input(\"Enter a product: \").lower()\n",
"\n",
" if product not in products:\n",
" print(\"Invalid product. Please try again.\")\n",
" continue\n",
"\n",
" if product in customer_orders:\n",
" print(\"You already ordered that product. Pick another one.\")\n",
" continue\n",
"\n",
" customer_orders.add(product)\n",
" print(f\"Added {product} to the order.\")\n",
"\n",
" choice = input(\"Do you want to add another product? (yes/no): \").lower()\n",
" while choice not in (\"yes\", \"no\"):\n",
" choice = input(\"Please answer 'yes' or 'no': \").lower()\n",
"\n",
" if choice == \"no\":\n",
" break\n",
"\n",
"print(f\"Customer order: {customer_orders}\")"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "971a1272-c1fb-486c-9482-dba688fb7e94",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter quantity for t-shirt: 4\n",
"Enter quantity for mug: 4\n",
"Enter quantity for hat: 4\n",
"Enter quantity for book: 4\n",
"Enter quantity for keychain: 4\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"book: 3\n"
]
}
],
"source": [
"inventory = {}\n",
"\n",
"for product in products:\n",
" quantity = int(input(f\"Enter quantity for {product}: \"))\n",
" inventory[product] = quantity\n",
"\n",
"for product in customer_orders: \n",
" inventory[product] -= 1\n",
" print(f\"{product}: {inventory[product]}\") "
]
}
],
"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 +208,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.5"
}
},
"nbformat": 4,
Expand Down