Skip to content
Open
Show file tree
Hide file tree
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
Binary file added .DS_Store
Binary file not shown.
98 changes: 96 additions & 2 deletions lab-python-flow-control.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,105 @@
"\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": "dc8121b5",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Enter initial inventory quantities:\n",
"\n",
"Inventory created: {'t-shirt': 5, 'mug': 5, 'hat': 5, 'book': 5, 'keychain': 5}\n",
"\n",
"Available products: ['t-shirt', 'mug', 'hat', 'book', 'keychain']\n",
"Added 't-shirt'. Current order (unique): {'t-shirt'}\n",
"Added 'mug'. Current order (unique): {'t-shirt', 'mug'}\n",
"Added 'hat'. Current order (unique): {'t-shirt', 'hat', 'mug'}\n",
"\n",
"Customer order set (unique): {'t-shirt', 'hat', 'mug'}\n",
"\n",
"Order Statistics:\n",
"Total Unique Products Ordered: 3\n",
"Percentage of Products Ordered: 60.00%\n",
"\n",
"Updated Inventory:\n",
"t-shirt: 4\n",
"mug: 4\n",
"hat: 4\n",
"book: 5\n",
"keychain: 5\n"
]
}
],
"source": [
"\n",
"products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"\n",
"inventory = {}\n",
"print(\"Enter initial inventory quantities:\")\n",
"for p in products:\n",
" while True:\n",
" try:\n",
" qty = int(input(f\"Enter the quantity of {p}: \"))\n",
" if qty < 0:\n",
" print(\"Please enter a non-negative number.\")\n",
" continue\n",
" inventory[p] = qty\n",
" break\n",
" except ValueError:\n",
" print(\"Please enter a valid integer.\")\n",
"\n",
"print(\"\\nInventory created:\", inventory)\n",
"\n",
"customer_orders = set()\n",
"print(\"\\nAvailable products:\", products)\n",
"\n",
"while True:\n",
" item = input(\"\\nEnter a product to add to the order: \").strip().lower()\n",
" if item in products:\n",
" customer_orders.add(item)\n",
" print(f\"Added '{item}'. Current order (unique): {customer_orders}\")\n",
" else:\n",
" print(\"Invalid product, please choose from:\", products)\n",
" # don't ask to continue on invalid; loop back to ask for a valid product\n",
" continue\n",
"\n",
" \n",
" another = input(\"Add another product? (yes/no): \").strip().lower()\n",
" if another in (\"no\", \"n\"):\n",
" break\n",
" elif another in (\"yes\", \"y\"):\n",
" continue\n",
" else:\n",
" print(\"Didn't catch that—stopping here.\")\n",
" break\n",
"\n",
"print(\"\\nCustomer order set (unique):\", customer_orders)\n",
"\n",
"total_unique_ordered = len(customer_orders)\n",
"percentage_ordered = (total_unique_ordered / len(products)) * 100 if products else 0.0\n",
"\n",
"print(\"\\nOrder Statistics:\")\n",
"print(f\"Total Unique Products Ordered: {total_unique_ordered}\")\n",
"print(f\"Percentage of Products Ordered: {percentage_ordered:.2f}%\")\n",
"\n",
"for item in customer_orders:\n",
" inventory[item] = max(0, inventory[item] - 1)\n",
"\n",
"print(\"\\nUpdated Inventory:\")\n",
"for product, qty in inventory.items():\n",
" print(f\"{product}: {qty}\")\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
Expand All @@ -55,7 +149,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.9.6"
}
},
"nbformat": 4,
Expand Down