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
145 changes: 143 additions & 2 deletions lab-python-functions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,152 @@
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "3386b41f",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"=== Initialize Inventory ===\n",
"\n",
"=== Take Customer Orders ===\n",
"\n",
"=== Update Inventory ===\n",
"- 't-shirt' deducted by 1. New stock: 4\n",
"\n",
"=== Order Statistics ===\n",
"- Total unique products ordered: 1\n",
"- % of unique catalog ordered: 20.00%\n",
"\n",
"=== Updated Inventory ===\n",
"- t-shirt: 4\n",
"- mug: 2\n",
"- hat: 3\n",
"- book: 1\n",
"- keychain: 4\n"
]
}
],
"source": [
"def initialize_inventory(products):\n",
" \"\"\"\n",
" Ask the user for a starting quantity for each product.\n",
" Returns a dictionary like: {'t-shirt': 5, 'mug': 3, ...}\n",
" \"\"\"\n",
" inventory = {}\n",
" print(\"\\n=== Initialize Inventory ===\")\n",
" for p in products:\n",
" while True:\n",
" raw = input(f\"Enter the quantity of '{p}': \").strip()\n",
" if raw.isdigit():\n",
" inventory[p] = int(raw)\n",
" break\n",
" else:\n",
" print(\"Please enter a non-negative integer (e.g., 0, 1, 2, ...).\")\n",
" return inventory\n",
"\n",
"\n",
"def get_customer_orders():\n",
" \"\"\"\n",
" Interactively collect product names from the user.\n",
" Returns a set of unique product names the customer wants.\n",
" \"\"\"\n",
" customer_orders = set()\n",
" print(\"\\n=== Take Customer Orders ===\")\n",
" while True:\n",
" item = input(\"Enter a product name (or press Enter to stop): \").strip().lower()\n",
" if item == \"\":\n",
" \n",
" break\n",
" customer_orders.add(item)\n",
"\n",
" \n",
" while True:\n",
" another = input(\"Add another product? (yes/no): \").strip().lower()\n",
" if another in (\"yes\", \"no\", \"y\", \"n\"):\n",
" break\n",
" print(\"Please type 'yes' or 'no'.\")\n",
" if another in (\"no\", \"n\"):\n",
" break\n",
"\n",
" return customer_orders\n",
"\n",
"\n",
"def update_inventory(customer_orders, inventory):\n",
" \"\"\"\n",
" For each product the customer ordered (unique), deduct 1 from inventory if available.\n",
" If a product doesn't exist or is out of stock, show a helpful message.\n",
" \"\"\"\n",
" print(\"\\n=== Update Inventory ===\")\n",
" for item in customer_orders:\n",
" if item not in inventory:\n",
" print(f\"- '{item}' is not in our product list. (Skipped)\")\n",
" continue\n",
" if inventory[item] > 0:\n",
" inventory[item] -= 1\n",
" print(f\"- '{item}' deducted by 1. New stock: {inventory[item]}\")\n",
" else:\n",
" print(f\"- '{item}' is out of stock. (No change)\")\n",
"\n",
"\n",
"def calculate_order_statistics(customer_orders, products):\n",
" \"\"\"\n",
" Calculate:\n",
" - total unique products ordered\n",
" - percentage of unique products ordered relative to total available products\n",
" Returns a tuple: (total_unique_ordered, percent_unique)\n",
" \"\"\"\n",
" total_unique = len(customer_orders)\n",
" total_products = len(products)\n",
" percent_unique = (total_unique / total_products * 100) if total_products else 0.0\n",
" return total_unique, percent_unique\n",
"\n",
"\n",
"def print_order_statistics(order_statistics):\n",
" \"\"\"\n",
" Nicely print the order statistics returned by calculate_order_statistics.\n",
" \"\"\"\n",
" total_unique, percent_unique = order_statistics\n",
" print(\"\\n=== Order Statistics ===\")\n",
" print(f\"- Total unique products ordered: {total_unique}\")\n",
" print(f\"- % of unique catalog ordered: {percent_unique:.2f}%\")\n",
"\n",
"\n",
"def print_updated_inventory(inventory):\n",
" \"\"\"\n",
" Print the updated inventory in a simple, readable format.\n",
" \"\"\"\n",
" print(\"\\n=== Updated Inventory ===\")\n",
" if not inventory:\n",
" print(\"(No inventory data.)\")\n",
" return\n",
" for product, qty in inventory.items():\n",
" print(f\"- {product}: {qty}\")\n",
"\n",
"\n",
"if __name__ == \"__main__\":\n",
" # You can adjust this list anytime\n",
" products = [\"t-shirt\", \"mug\", \"hat\", \"book\", \"keychain\"]\n",
"\n",
" inventory = initialize_inventory(products)\n",
" customer_orders = get_customer_orders()\n",
" update_inventory(customer_orders, inventory)\n",
"\n",
" stats = calculate_order_statistics(customer_orders, products)\n",
" print_order_statistics(stats)\n",
" print_updated_inventory(inventory)\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
Expand All @@ -61,7 +202,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.9.6"
}
},
"nbformat": 4,
Expand Down