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
71 changes: 69 additions & 2 deletions lab-python-flow-control.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,78 @@
"\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": 2,
"id": "81e29d71",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"pants is not a valid product.\n",
"socks is not a valid product.\n",
"Products in the customer orders:\n",
"- hat\n",
"- mug\n",
"Order Statistics:\n",
"Total Products Ordered: 2\n",
"Percentage of Products Ordered: 40.00%\n",
"Updated Inventory:\n",
"t-shirt: 5\n",
"mug: 4\n",
"hat: 4\n",
"book: 5\n",
"keychain: 5\n"
]
}
],
"source": [
"# ## Exercise: Managing Customer Orders\n",
"\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"inventory = {}\n",
"\n",
"for product in products:\n",
" quantity = int(input(f\"Enter the quantity for {product}: \"))\n",
" inventory[product] = quantity\n",
"\n",
"customer_orders = set()\n",
"\n",
"while answer := input(\"Do you want to add a product to your order? (yes/no): \") == \"yes\":\n",
" order = input(f\"Enter the name of product {len(customer_orders) + 1} to order: \")\n",
" if order in products:\n",
" customer_orders.add(order)\n",
" else:\n",
" print(f\"{order} is not a valid product.\")\n",
"\n",
"print(\"Products in the customer orders:\")\n",
"for item in customer_orders:\n",
" print(f\"- {item}\")\n",
"\n",
"total_products_ordered = len(customer_orders)\n",
"percentage_ordered = (total_products_ordered / len(products)) * 100\n",
"order_status = (total_products_ordered, percentage_ordered)\n",
"\n",
"print(\"Order Statistics:\")\n",
"print(f\"Total Products Ordered: {order_status[0]}\")\n",
"print(f\"Percentage of Products Ordered: {order_status[1]:.2f}%\")\n",
"\n",
"for item in customer_orders:\n",
" if item in inventory:\n",
" inventory[item] -= 1\n",
"\n",
"print(\"Updated Inventory:\")\n",
"for item, quantity in inventory.items():\n",
" print(f\"{item}: {quantity}\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
Expand All @@ -55,7 +122,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.6"
}
},
"nbformat": 4,
Expand Down