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
84 changes: 82 additions & 2 deletions lab-python-functions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,91 @@
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "4482555e",
"metadata": {},
"outputs": [],
"source": [
"# .1 Initialize inventory from product list and user input\n",
"def initialize_inventory(products):\n",
" inventory = {}\n",
" for product in products:\n",
" qty = int(input(f\"Enter quantity for {product}: \"))\n",
" inventory[product] = qty\n",
" return inventory\n",
"\n",
"# .2 Collect customer orders in a loop\n",
"def get_customer_orders():\n",
" customer_orders = set()\n",
" while True:\n",
" product = input(\"Enter the name of a product to order: \").strip().lower()\n",
" customer_orders.add(product)\n",
" another = input(\"Do you want to add another product? (yes/no): \").strip().lower()\n",
" if another != 'yes':\n",
" break\n",
" return customer_orders\n",
"\n",
"# .3 Update inventory based on orders\n",
"def update_inventory(customer_orders, inventory):\n",
" for product in customer_orders:\n",
" if product in inventory and inventory[product] > 0:\n",
" inventory[product] -= 1\n",
" elif product in inventory:\n",
" print(f\"{product} is out of stock!\")\n",
" else:\n",
" print(f\"{product} not found in inventory.\")\n",
"\n",
"# .4 Calculate order statistics (total, percentage)\n",
"def calculate_order_statistics(customer_orders, products):\n",
" total_ordered = len(customer_orders)\n",
" percent_unique = (total_ordered / len(products)) * 100 if products else 0\n",
" return total_ordered, percent_unique\n",
"\n",
"# .5 Print order statistics\n",
"def print_order_statistics(order_statistics):\n",
" total_ordered, percent_unique = order_statistics\n",
" print(f\"Total unique products ordered: {total_ordered}\")\n",
" print(f\"Percentage of unique products ordered: {percent_unique:.2f}%\")\n",
"\n",
"# .6 Print updated inventory\n",
"def print_updated_inventory(inventory):\n",
" print(\"Updated inventory:\")\n",
" for product, qty in inventory.items():\n",
" print(f\"{product}: {qty}\")\n",
"\n",
"# .7 Run all functions in sequence\n",
"products = ['apple', 'banana', 'orange', 'grape']\n",
"inventory = initialize_inventory(products)\n",
"customer_orders = get_customer_orders()\n",
"update_inventory(customer_orders, inventory)\n",
"order_statistics = calculate_order_statistics(customer_orders, products)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "8eceb80d",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'apple': 21, 'banana': 20, 'orange': 2, 'grape': 3}\n"
]
}
],
"source": [
"print(inventory)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -61,7 +141,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.5"
}
},
"nbformat": 4,
Expand Down