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
114 changes: 112 additions & 2 deletions lab-python-flow-control.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,116 @@
"# Lab | Flow Control"
]
},
{
"cell_type": "markdown",
"id": "d8cb3fe6",
"metadata": {},
"source": [
"## Exercise: Managing Customer Orders Optimized"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a51bae6c",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"t-shirt: *5\n",
"mug: *2\n",
"hat: *5\n",
"book: *2\n",
"keychain: *5\n",
"Current customer orders (empty set): set()\n",
"Invalid product, this item will be ignored.\n",
"\n",
"Products in the customer's order (set):\n",
"\n",
"*Order Statistics:*\n",
"Total Products Ordered (distinct): 0\n",
"Percentage of Products Ordered: 0.00%\n",
"\n",
"*Updated Inventory*\n",
"t-shirt: 5\n",
"mug: 2\n",
"hat: 5\n",
"book: 2\n",
"keychain: 5\n"
]
}
],
"source": [
"\n",
"#1. Look at your code from the lab data structures, and improve repeated code with loops:\n",
"\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"\n",
"inventory = {}\n",
"\n",
"for product in products:\n",
" quantity = int(input(f\"Enter the available quantity for {product}: \")) #f strings: to format selected parts of a string\n",
" inventory[product] = quantity\n",
" print(f\"{product}: {inventory[product]}\")\n",
"\n",
"\n",
"customer_orders = set()\n",
"print(\"Current customer orders (empty set):\", customer_orders)\n",
"\n",
"#2. Instead of asking the user to input the name of three products that a customer wants to order, do the following:\n",
"# a. Prompt the user to enter the name of a product that a customer wants to order:\n",
"# b. Add the product name to the \"customer_orders\" set:\n",
"# c. Ask the user if they want to add another product (yes/no):\n",
"# d. Continue the loop until the user does not want to add another product:\n",
"\n",
"while True:\n",
" product_name = input(\"Enter the name of a product to add to the order: \").strip().lower()\n",
"\n",
" if product_name in products:\n",
" customer_orders.add(product_name)\n",
" print(f\"{product_name} added to the order.\")\n",
" else:\n",
" print(\"Invalid product, this item will be ignored.\")\n",
"\n",
" another_product = input(\"Do you want to add another product? (yes/no): \").strip().lower()\n",
" if another_product not in (\"yes\", \"y\", \"Y\", \"YES\", \"Yes\"):\n",
" break\n",
"\n",
" \n",
"print(\"\\nProducts in the customer's order (set):\") # \\n to create a new line\n",
"for item in customer_orders:\n",
" print(\"-\", item)\n",
"\n",
"# Order statistics:\n",
"total_products_ordered = len(customer_orders)\n",
"\n",
"# % of products ordered compared to the total number of different products:\n",
"percentage_ordered = (total_products_ordered / len(products)) * 100\n",
"\n",
"order_status = (total_products_ordered, percentage_ordered)\n",
"\n",
"# Print the order statistics:\n",
"print(\"\\n*Order Statistics:*\")\n",
"print(f\"Total Products Ordered (distinct): {order_status[0]}\")\n",
"print(f\"Percentage of Products Ordered: {order_status[1]:.2f}%\")\n",
"\n",
"# 3. Instead of updating the inventory by subtracting 1 from the quantity of each product, \n",
"# only do it for the products that were ordered (those in \"customer_orders\"):\n",
"\n",
"for ordered_product in customer_orders:\n",
" if inventory[ordered_product] > 0:\n",
" inventory[ordered_product] -= 1\n",
" else:\n",
" print(f\"Warning: no inventory left for '{ordered_product}'.\")\n",
"\n",
"\n",
"print(\"\\nUpdated Inventory\")\n",
"for product in products:\n",
" print(f\"{product}: {inventory[product]}\")"
]
},
{
"cell_type": "markdown",
"id": "3851fcd1-cf98-4653-9c89-e003b7ec9400",
Expand Down Expand Up @@ -41,7 +151,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -55,7 +165,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.5"
}
},
"nbformat": 4,
Expand Down