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
63 changes: 62 additions & 1 deletion lab-python-flow-control.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,67 @@
"\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": "7e7b83a8-e8bc-4e12-8854-d642bd7178f0",
"metadata": {},
"outputs": [],
"source": [
"# 1. Define list of products\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"\n",
"# 2. Create empty dictionary for inventory\n",
"inventory = {}\n",
"\n",
"# 3. Get quantity for each product from user input\n",
"print(\"--- Enter Inventory Quantities ---\")\n",
"for product in products:\n",
" quantity = int(input(f\"Enter quantity for '{product}': \"))\n",
" inventory[product] = quantity\n",
"\n",
"# 4. Create empty set for customer orders\n",
"customer_orders = set()\n",
"\n",
"# 5. Get product orders dynamically from the user until they want to stop\n",
"print(\"\\n--- Enter Customer Orders ---\")\n",
"user_choice = \"yes\"\n",
"\n",
"while user_choice == \"yes\":\n",
" # a. Prompt the user to enter the name of a product\n",
" order = input(\"Enter the name of a product that a customer wants to order: \").strip().lower()\n",
" \n",
" # b. Add the product name to the \"customer_orders\" set\n",
" customer_orders.add(order)\n",
" \n",
" # c. Ask the user if they want to add another product (yes/no)\n",
" user_choice = input(\"Do you want to add another product? (yes/no): \").strip().lower()\n",
"\n",
"# 6. Print the products in customer_orders\n",
"print(\"\\nCustomer Orders:\")\n",
"print(customer_orders)\n",
"\n",
"# 7. Calculate order statistics and store in a tuple\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",
"# 8. Print order statistics in the required format\n",
"print(\"\\nOrder Statistics:\")\n",
"print(f\"Total Products Ordered: {order_status[0]}\")\n",
"print(f\"Percentage of Products Ordered: {order_status[1]:.1f}%\")\n",
"\n",
"# 9. Update the inventory by subtracting 1 for ordered items\n",
"for product in customer_orders:\n",
" if product in inventory:\n",
" inventory[product] -= 1\n",
"\n",
"# 10. Print updated inventory on separate lines\n",
"print(\"\\nUpdated Inventory:\")\n",
"for product, quantity in inventory.items():\n",
" print(f\"{product}: {quantity}\")\n"
]
}
],
"metadata": {
Expand All @@ -55,7 +116,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.14.6"
}
},
"nbformat": 4,
Expand Down