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
81 changes: 79 additions & 2 deletions lab-python-flow-control.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,88 @@
"\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": 1,
"id": "5990ea77",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Product not available. Try again.\n",
"{'book', 'hat'}\n",
"Order Statistics:\n",
"Total Products Ordered: 2\n",
"Percentage of Products Ordered: 40.0%\n",
"Updated Inventory:\n",
"t-shirt: 10\n",
"mug: 10\n",
"hat: 9\n",
"book: 9\n",
"keychain: 10\n"
]
}
],
"source": [
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"\n",
"inventory = {}\n",
"\n",
"# Ask inventory quantities using a loop\n",
"for product in products:\n",
" quantity = int(input(f\"How many {product}s are available? \"))\n",
" inventory[product] = quantity\n",
"\n",
"customer_orders = set()\n",
"\n",
"# Ask customer orders using a while loop\n",
"while True:\n",
"\n",
" while True:\n",
" product = input(\"Enter a product to order: \").lower()\n",
"\n",
" if product in products:\n",
" customer_orders.add(product)\n",
" break\n",
" else:\n",
" print(\"Product not available. Try again.\")\n",
"\n",
" answer = input(\"Do you want to add another product? (yes/no): \")\n",
"\n",
" if answer.lower() == \"no\":\n",
" break\n",
"\n",
"print(customer_orders)\n",
"\n",
"# Calculate statistics\n",
"total_products_ordered = len(customer_orders)\n",
"percentage_ordered = (total_products_ordered / len(products)) * 100\n",
"\n",
"order_status = (total_products_ordered, percentage_ordered)\n",
"\n",
"# Print statistics\n",
"print(\"Order Statistics:\")\n",
"print(f\"Total Products Ordered: {order_status[0]}\")\n",
"print(f\"Percentage of Products Ordered: {order_status[1]}%\")\n",
"\n",
"# Update inventory only for ordered products\n",
"for product in customer_orders:\n",
" if product in inventory:\n",
" inventory[product] -= 1\n",
"\n",
"# Print updated inventory\n",
"print(\"Updated Inventory:\")\n",
"for product in inventory:\n",
" print(f\"{product}: {inventory[product]}\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -55,7 +132,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.9"
}
},
"nbformat": 4,
Expand Down