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
119 changes: 118 additions & 1 deletion lab-python-flow-control.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,123 @@
"\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": 5,
"id": "81c99d70-613c-4ece-b9e0-a0c50189afb4",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter quantity: 4\n",
"Enter quantity: 5\n",
"Enter quantity: 6\n",
"Enter quantity: 4\n",
"Enter quantity: 7\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'t-shirt': 4, 'mug': 5, 'hat': 6, 'book': 4, 'keychain': 7}\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter your order: book\n",
"Do you want to add another product (yes/no)? yes\n",
"Enter your order: mug\n",
"Do you want to add another product (yes/no)? yes\n",
"Enter your order: dsfdsfds\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Product not found. Try again.\n"
]
},
{
"name": "stdin",
"output_type": "stream",
"text": [
"Enter your order: t-shirt\n",
"Do you want to add another product (yes/no)? No\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Customer orders: {'mug', 't-shirt', 'book'}\n",
"Order Statistics:\n",
"Total Products Ordered: 3\n",
"Percentage of Products Ordered: 60.0%\n",
"t-shirt: 3\n",
"mug: 4\n",
"hat: 6\n",
"book: 3\n",
"keychain: 7\n"
]
}
],
"source": [
"# 1.\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"# 2.\n",
"inventory = {}\n",
"# 3.\n",
"for product in products:\n",
" inventory[product] = int(input(\"Enter quantity: \"))\n",
"\n",
"print(inventory)\n",
"# 4.\n",
"customer_orders = set()\n",
"# 5.\n",
"while True:\n",
" order = input(f\"Enter your order: \")\n",
" while order not in products:\n",
" print(\"Product not found. Try again.\")\n",
" order = input(f\"Enter your order: \")\n",
" customer_orders.add(order)\n",
" add_another = input(\"Do you want to add another product (yes/no)?\").lower()\n",
" if add_another == \"no\":\n",
" break\n",
"# 6.\n",
"print(f\"Customer orders: {customer_orders}\")\n",
"# 7.\n",
"total_products_ordered = len(customer_orders)\n",
"percentage = (total_products_ordered / len(products)) * 100\n",
"order_status = (total_products_ordered, percentage)\n",
"\n",
"# 8.\n",
"print(\"Order Statistics:\")\n",
"print(f\"Total Products Ordered: \", total_products_ordered)\n",
"print(f\"Percentage of Products Ordered: {percentage}%\") \n",
"\n",
"# 9.\n",
"for product in customer_orders:\n",
" inventory[product] -= 1\n",
"\n",
"# 10.\n",
"for product, quantity in inventory.items():\n",
" print(f\"{product}: {quantity}\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f0228b67-6b6e-4126-ae7e-0a4e00865970",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand All @@ -55,7 +172,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.14.0"
}
},
"nbformat": 4,
Expand Down